boost asio ignore header
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;
}
}
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line