2017-12-08 10 views
1

私は株価情報を取得し、Google検索を実行し、データ(現在の価格、高値、安値、変動率など)を出力するプログラムを作成しようとしています。 。 boost asioを使用しようとしていて、サーバーからデータを返さない。boost asioを使用してウェブページを取得

#include "stdafx.h" 
#include <iostream> 
#include <istream> 
#include <ostream> 
#include <string> 
#include <boost/asio.hpp> 

std::string getStockPage(std::string ticker) { 
    boost::asio::ip::tcp::iostream stream; 

    stream.connect("www.google.com", "http"); 
    std::cout << "connected\n"; 
    stream << "GET /search?q=" << ticker << " HTTP/1.1\r\n"; 
    stream << "Host: www.google.com\r\n"; 
    stream << "Cache-Control: no-cache\r\n"; 
    //stream << "Content-Type: application/x-www-form-urlencoded\r\n\r\n"; 
    stream << "Connection: close\r\n\r\n"; 
    std::cout << "sent\n"; 

    std::ostringstream os; 
    //os << stream.rdbuf(); 
    char buffer[100]; 
    os << stream.readsome(buffer, 100); 
    return std::string(buffer, 100); 
} 

int main() { 
    std::cout << getStockPage("$tsla"); 
    std::cout << "done\n"; 
    std::string temp; 
    std::getline(std::cin, temp); 
    return 0; 


} 

私はそれが応答を出力する問題を抱えていたかどうかを確認するためにちょうど最初の100個の文字を読み込もうとしましたが、それだけでヌル文字を出力します。私はそれがGoogleのページ全体を出力するようにしたい "www.google.com/search?q=$tsla"

ご協力いただければ幸いです!

+0

を印刷されたプログラムhttp://www.boost.org/doc/libs/1_65_1/doc/:あなたはフラッシュを追加することができますhtml/boost_asio/example/cpp03/http/client/sync_client.cpp –

+0

[cURLに類似したboost :: asioを使用したhttp GETリクエストの送信]の可能な複製(https://stackoverflow.com/questions/28728347/sending-http -get-request-using-boostasio-curl-like-curl) –

答えて

1

std::istream::readsomeは常に0バイトを返します。あなたはNULバイトを受け取ったかのように、それはあなたが代わりに

return std::string(buffer, stream.gcount()); 

return std::string(buffer, 100); 

をしたので、本当に、ただ

std::ostringstream os; 
os << stream.rdbuf(); 
return os.str(); 

他のアプローチを使用する。これは、のために働く、表示されます私はテストのとき。

stream << "Connection: close\r\n\r\n" << std::flush; 

#include <boost/asio.hpp> 
#include <iostream> 
#include <string> 

std::string getStockPage(std::string const& ticker) { 
    boost::asio::ip::tcp::iostream stream; 

    stream.connect("www.google.com", "http"); 
    stream << "GET /search?q=" << ticker << " HTTP/1.1\r\n"; 
    stream << "Host: www.google.com\r\n"; 
    stream << "Cache-Control: no-cache\r\n"; 
    // stream << "Content-Type: application/x-www-form-urlencoded\r\n\r\n"; 
    stream << "Connection: close\r\n\r\n" << std::flush; 

    std::ostringstream os; 
    os << stream.rdbuf(); 
    return os.str(); 
} 

int main() { 
    std::cout << getStockPage("$tsla"); 
} 

HTTP/1.1 302 Found 
Location: http://www.google.nl/search?q=%24tsla&gws_rd=cr&dcr=0&ei=3EMqWrKxCILUwAKv9LqICg 
Cache-Control: private 
Content-Type: text/html; charset=UTF-8 
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info." 
Date: Fri, 08 Dec 2017 07:48:44 GMT 
Server: gws 
Content-Length: 288 
X-XSS-Protection: 1; mode=block 
X-Frame-Options: SAMEORIGIN 
Set-Cookie: NID=118=MsVZZpoZFEz4mQDqDuuWFRViB8v8yEQju7FPdOw8Rr7ViQ1cJtF6ZeN9u-dSRhGMT4x8F8yDilk9FqsoTkO8IsoQX-YvHXRcCoHcOLk0p4VOTn8AZoldKeh84Ryl0bM0; expires=Sat, 09-Jun-2018 07:48:44 GMT; path=/; domain=.google.com; HttpOnly 
Connection: close 

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<TITLE>302 Moved</TITLE></HEAD><BODY> 
<H1>302 Moved</H1> 
The document has moved 
<A HREF="http://www.google.nl/search?q=%24tsla&amp;gws_rd=cr&amp;dcr=0&amp;ei=3EMqWrKxCILUwAKv9LqICg">here</A>. 
</BODY></HTML> 
関連する問題