2017-12-08 21 views
1

주식 시세 표시, Google 검색 실행 및 데이터 출력 (현재 가격, 고가, 저가, 변경 비율 등)을 구축하려고합니다. . 부스트 asio 사용하려고하고 서버에서 모든 데이터를 반환하지 않습니다. 부스트 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; 


} 

는 I는 상기 응답을 출력하는 문제가되지 않았는지 단지 처음 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-like-to-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>