转发地址:https://blog.csdn.net/jinchaoh/article/details/47438911
HTTP(超文本传输协议)是一种客户端与服务端的传输协议,最早用于浏览器和服务器之间的通信,后来因为其使用灵活、方便等特点,广泛用于客户端与服务端的通信。文章将简单介绍HTTP协议,同时以C++方式分别实现HTTP GET、POST 请求
HTTP 请求报文
HTTP请求报文的一般格式由4部分组成:请求行、请求头部、空行、请求数据。如下图所示:
请求行:包含3部分内容:请求方法,URL,协议版本。形式如:GET /?aaa=1 HTTP/1.1。请求方法有GET、POST、HEAD、PUT、DELETE、OPTIONS等。URL指请求服务端的地址,可以是相对地址或域名形式的绝对地址。协议版本主要有HTTP/1.1 HTTP/1.0 HTTP/0.9,后面两种已很少使用了。
请求头部:以key/value形式成对表示头部参数,以英文冒号分隔。key名称的约定写法为Key,Key-Name,自定义key名称一般以“X-”开头。如php的声明“X-Powered-By:PHP/5.5.4-1”
空行:用来标识请求头部的数据已结束。
请求数据:可选项,这块内容只在POST方式下使用,作为POST的数据表示区域。使用这块内容,要在请求头部以Content-Length声明请求数据长度,以Content-Type声明请求数据类型。
HTTP POST请求
HTTP POST方式是把请求参数放到HTTP请求报文的请求数据中,为了让例子更容易看懂,仅保留HTTP Post关键参数,你还可以自定义一些参数,比如浏览器喜欢用的User-Agent,Accept,Connection等等
char *pHttpPost = "POST %s HTTP/1.1 "
"Host: %s:%d "
"Content-Type: application/x-www-form-urlencoded "
"Content-Length: %d "
"%s";
char* addr = "http://localhost/post.php";char* host = "127.0.0.1";int port = 80;char* msg = "aaa=1&bbb=2";
char strHttpPost[1024] = {0};sprintf(strHttpPost, pHttpPost, addr, host, port, strlen(msg), msg);
//这里忽略掉了socket连接代码
send(sockClient, strHttpPost, strlen(strHttpPost), 0);
HTTP GET请求
HTTP GET方式是把请求参数放到HTTP请求报文的请求行URL中,所以请求行就是“GET /?aaa=1&bbb=2 HTTP/1.1 ”。URL最大长度通常浏览器取255,这和文件路径最大长度有关。虽然HTTP允许更大长度,但不建议怎么做,如果太长了,可以考虑换成POST方式
char *pHttpGet = "GET %s?%s HTTP/1.1 "
"Host: %s:%d ";
char* addr = "http://localhost/get.php";char* host = "127.0.0.1";int post = 80;char* msg = "aaa=1&bbb=2";
char strHttpGet[1024] = {0};sprintf(strHttpGet, pHttpGet, addr, msg, host, post);
//这里忽略掉了socket连接代码
send(sockClient, strHttpGet, strlen(strHttpGet), 0);
实现的HTTP
#include "HttpConnect.h"
#ifdef WIN32#pragma comment(lib,"ws2_32.lib")#endifHttpConnect::HttpConnect(){#ifdef WIN32
//此处一定要初始化一下,否则gethostbyname返回一直为空
WSADATA wsa = { 0 };
WSAStartup(MAKEWORD(2, 2), &wsa);#endif}HttpConnect::~HttpConnect(){}void HttpConnect::socketHttp(std::string host, std::string request){
int sockfd;
struct sockaddr_in address;
struct hostent *server;
sockfd = socket(AF_INET,SOCK_STREAM,0);
address.sin_family = AF_INET;
address.sin_port = htons(80);
server = gethostbyname(host.c_str());
memcpy((char *)&address.sin_addr.s_addr,(char*)server->h_addr, server->h_length);
if(-1 == connect(sockfd,(struct sockaddr *)&address,sizeof(address))){
DBG <<"connection error!"<<std::endl;
return;
}
DBG << request << std::endl;#ifdef WIN32
send(sockfd, request.c_str(),request.size(),0);#else
write(sockfd,request.c_str(),request.size());#endif
char buf[1024*1024] = {0};
int offset = 0;
int rc;#ifdef WIN32
while(rc = recv(sockfd, buf+offset, 1024,0))#else
while(rc = read(sockfd, buf+offset, 1024))#endif
{
offset += rc;
}#ifdef WIN32
closesocket(sockfd);#else
close(sockfd);#endif
buf[offset] = 0;
DBG << buf << std::endl;}void HttpConnect::postData(std::string host, std::string path, std::string post_content){
//POST请求方式
std::stringstream stream;
stream << "POST " << path;
stream << " HTTP/1.0 ";
stream << "Host: "<< host << " ";
stream << "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ";
stream << "Content-Type:application/x-www-form-urlencoded ";
stream << "Content-Length:" << post_content.length()<<" ";
stream << "Connection:close ";
stream << post_content.c_str();
socketHttp(host, stream.str());}void HttpConnect::getData(std::string host, std::string path, std::string get_content){
//GET请求方式
std::stringstream stream;
stream << "GET " << path << "?" << get_content;
stream << " HTTP/1.0 ";
stream << "Host: " << host << " ";
stream <<"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ";
stream <<"Connection:close ";
socketHttp(host, stream.str());}
HttpConnect *http = new HttpConnect();
http->getData("127.0.0.1", "/login", "id=liukang&pw=123");
http->postData("127.0.0.1", "/login","id=liukang&pw=123");