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;

}




     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1953681,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值