我有一个代码来获取html文件的来源,但它也得到“标题响应”或某事。那样的(我不知道如何调用它)如果没有这个标题我怎么能得到它?
我的代码:
#include "StdAfx.h"
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::size_t completion(const boost::system::error_code& error, std::size_t bytes_transfered)
{
return ! error;
}
int main(int argc, char* argv[])
{
boost::asio::io_service io_service;
boost::asio::ip::address addr = boost::asio::ip::address::from_string("31.170.161.16");
boost::asio::ip::tcp::endpoint endpoint(addr, 80);
tcp::socket socket(io_service);
socket.connect(endpoint);
boost::asio::streambuf request;
std::ostream requestStream(&request);
requestStream << "GET /xD1azt4_panel/bhc.html HTTP/1.1\r\n"
<< "Connection: Keep-Alive\r\n"
<< "Host: dublersoft.hostoi.com\r\n\r\n";
boost::asio::write(socket, request);
boost::asio::streambuf respond;
boost::system::error_code ec;
boost::asio::read(socket, respond, completion, ec);
std::cout << &respond << std::endl;
getchar();
return 0;
}
结果:
答案 0 :(得分:4)
令我感到惊讶的是,我也使用它作为我的简单http客户端,所以我只是分享我的方法,如果你以后需要它,头文件放在一个向量中,响应数据放在ostream对象中。
int do_get(std::string &host_,std::string &port_, std::string url_path,std::ostream &out_,std::vector<std::string> &headers, unsigned int timeout)
{
try{
using namespace boost::asio::ip;
tcp::iostream request_stream;
if (timeout>0){
request_stream.expires_from_now(boost::posix_time::milliseconds(timeout));
}
request_stream.connect(host_,port_);
if(!request_stream){
return -1;
}
request_stream << "GET " << url_path << " HTTP/1.0\r\n";
request_stream << "Host: " << host_ << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Cache-Control: no-cache\r\n";
request_stream << "Connection: close\r\n\r\n";
request_stream.flush();
std::string line1;
std::getline(request_stream,line1);
if (!request_stream)
{
return -2;
}
std::stringstream response_stream(line1);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream,status_message);
if (!response_stream||http_version.substr(0,5)!="HTTP/")
{
return -1;
}
if (status_code!=200)
{
return (int)status_code;
}
std::string header;
while (std::getline(request_stream, header) && header != "\r")
headers.push_back(header);
out_ << request_stream.rdbuf();
return status_code;
}catch(std::exception &e){
std::cout << e.what() << std::endl;
return -3;
}
}
答案 1 :(得分:1)
答案是,如果你想在标题和正文之间分开,你将不得不使用更高级别的东西。
另一种方法是扫描\ r \ n \ r \ n这是标题/正文之间的分隔符,正如在官方http请求示例中所做的那样,您可以找到here
答案 2 :(得分:0)
Web服务器将始终返回至少几个标题行 - RFC需要一些(至少HTTP...
和Server:...
)。
第一个空白行标记标题的结尾。在那之后是身体。您需要阅读,但显然要忽略所有数据,直到第一个空白行,并在此之后保留所有内容。