一个HTTP请求报文由请求行(request line)、请求头部(header)、空行和请求数据4个部分组成
使用C++组装上述报文
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "POST /cs/restfull/operationRestfullApi/excuteSqlByCode HTTP/1.1\r\n";
request_stream << "Host: 192.168.0.88:8080\r\n";
//request_stream << "Connection: keep-alive\r\n";
request_stream << "Content-Length: " << strEncodeBuffer.size() << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
//request_stream << "User-Agent: Mozilla/4.0\r\n";
//request_stream << "Accept-Language: zh-CN\r\n";
request_stream << "\r\n";
request_stream << strEncodeBuffer;
请求行
request_stream << "POST /cs/restfull/operationRestfullApi/excuteSqlByCode HTTP/1.1\r\n";
请求头部
request_stream << "Host: 192.168.0.88:8080\r\n";
request_stream << "Connection: keep-alive\r\n";
request_stream << "Content-Length: " << strEncodeBuffer.size() << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
//request_stream << "User-Agent: Mozilla/4.0\r\n";
//request_stream << "Accept-Language: zh-CN\r\n";
空行
request_stream << "\r\n";
说明:最后一个请求头之后是一个空行,发送回车符和换行符,通知服务器以下不再有请求头
请求数据
request_stream << strEncodeBuffer;
说明:请求数据不在GET方法中使用,而是在POST方法中使用。
POST方法适用于需要客户填写表单的场合。与请求数据相关的最常使用的请求头是Content-Type和Content-Length。
参考
http://blog.csdn.net/zhangliang_571/article/details/23508953
本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1956054,如需转载请自行联系原作者