How to send a HTTP1.1 get POST request package using the socket
HTTP messages are text-oriented, and each field in a message is an ASCII string, and the length of each field is indeterminate. HTTP has two kinds of messages: Request message and Response message.
Request message
An HTTP request message consists of 4 parts of the request line, the request header (header), the blank line and the request data, and the following figure gives the general format of the request message.
(1) Request line
The request line consists of 3 fields in the Request Method field, the URL field, and the HTTP protocol version field, separated by a space. For example, get/index.html http/1.1.
The HTTP protocol request method has get, POST, head, put, DELETE, OPTIONS, TRACE, CONNECT. The most commonly used get methods and post methods are described here.
Get: When the client wants to read the document from the server, use the Getting method. The Get method requires the server to place the URL-positioned resource in the data section of the response message and send it back to the client. When using the Get method, the request parameters and corresponding values are appended to the URL, using a question mark ("?"). Represents the end of the URL and the start of the request parameter, and the length of the pass parameter is restricted. For example,/index.jsp?id=100&op=bind.
Post: The Post method can be used when the client provides more information to the server. The Post method encapsulates the request parameters in the HTTP request data in the form of a name/value that transmits large amounts of data that can be used to transfer files.
(2) Request head
The request header is composed of a keyword/value pair, a pair of each row, and the keywords and values are separated by the English colon ":". The request header notifies the server that there is information about the client request, and the typical request headers are:
User-agent: The browser type that generated the request.
Accept: List of content types that clients can recognize.
Host: The requested hostname allows multiple domain names to be in the same IP address as the virtual host.
(3) Blank line
After the last request header is a blank line, send a carriage return and a newline character to notify the server that there is no longer a request header.
Blank lines are required for a complete HTTP request, otherwise the server will assume that the data for this request has not been fully sent to the server and is in a waiting state.
(4) Request data
The request data is not used in the Get method, but is used in the Post method. The Post method is useful for situations where a customer needs to fill out a form. The most frequently used request headers associated with requesting data are Content-type and content-length.
(5) Sample Request
POST:
Post message headers are as follows:
post/sn/index.php http/1.1
Accept: */*
Accept-language:zh-cn
Host:localhost
content-type:application/x-www-form-urlencoded
Content-length:12
Connection:close
Sn=123&n=asa
There is a blank line behind the HTTP header, and then the post data is sent behind the blank line, and the length passes through the Content-length:12
Indicates that this post data contains two items
Sn=123
N=asa
Where: content-type:application/x-www-form-urlencoded Specifies the type of encoding for post data
Length of Content-length:12 post data
Get:
Get newspaper ask the head as follows:
Get/sn/index.php?sn=123&n=asa http/1.1
Accept: */*
Accept-language:zh-cn
Host:localhost
content-type:application/x-www-form-urlencoded
Content-length:12
Connection:close
Sample code:
void Mef_set_http_header (Mef_http_action_t method, S8 * Action, S8 * server, S8 * msg_body, S8 * head, U8 Connect_type)
{
S8 TMP_BUF[20];
Const S8 * Http_methods_table[mef_total_http_actions] = {' Get ', ' POST ', ' head ', ' Put ', ' options ', ' DELETE ', ' TRACE ', ' CONN ECT "};
sprintf (Head, "%s", Http_methods_table[method]);
if (action)
{
Strcat (head, action);
}
Strcat (head, "http/1.1");
Strcat (head, "/r/n");
Strcat (head, "accept:*/*");
Strcat (head, "/r/n");
Strcat (head, "user-agent:mozilla/4.0" (compatible; MSIE 5.01; Windows NT 5.0) ");
Strcat (head, "/r/n");
Strcat (Head, "Host:");
strcat (Head,server);
Strcat (head, "/r/n");
if (method = = Mef_http_post)
{
Strcat (head, "content-type:application/x-www-form-urlencoded");
Strcat (head, "/r/n");
}
if (method = = Mef_http_post)
{
Strcat (Head, "content-length:");
sprintf (Tmp_buf, "%d", strlen (Msg_body));
Strcat (head, TMP_BUF);
Strcat (head, "/r/n");
}
if (Connect_type = 1)
Strcat (head, "connection:keep-alive");
Else
Strcat (head, "Connection:close");
Strcat (head, "/r/n");
Strcat (head, "/r/n");
if (method = = Mef_http_post)
{
if (msg_body)
{
strcat (Head,msg_body);
}
}
}