asio 简单的http测试例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asio 简单的http测试例子相关的知识,希望对你有一定的参考价值。
#include <iostream>
#include <fstream>
#include <string>
#include <boost/asio.hpp>
using namespace std;
using namespace boost::asio;
int httptest()
{
io_service iosev;
ip::tcp::socket socket(iosev);
ip::tcp::endpoint ep(ip::address_v4::from_string("192.168.0.88"), 8080);
boost::system::error_code ec;
socket.connect(ep, ec);
if (ec) return -1;
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET /cs/restfull/operationRestfullApi/testGet/ HTTP/1.1\r\n";
request_stream << "Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
request_stream << "Accept-Language: zh-CN\r\n";
request_stream << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)\r\n";
request_stream << "Host: " << "192.168.0.88:8080" << "\r\n";
request_stream << "Connection: Keep-Alive\r\n";
boost::asio::write(socket, request);
char buf[1024] = { 0 };
size_t len = socket.read_some(buffer(buf), ec);
ofstream ofs;
ofs.open("httpresponse.txt");
ofs << buf << endl;
ofs.close();
return 0;
}
以上是关于asio 简单的http测试例子的主要内容,如果未能解决你的问题,请参考以下文章