HTTP working principle and interpretation of HTTP request and response message

Source: Internet
Author: User
Tags bind http request response code socket

original link: http://blog.csdn.net/laoyang360/article/details/8280478

first, the HTTP principle

1.HTTP Working principle (example Analysis):

<1> HTTP client establishes TCP connection with HTTP server;


<2> HTTP client sends HTTP connection request to HTTP server 80 port via browser;


<3> if the HTTP server allows access, the connection acknowledgement is returned, and both sides establish an HTTP connection;

<4>. HTTP client sends HTTP page request (HTTP Analyzer V5 analysis result)


<5>. The HTTP server looks for the HTTP file and returns the hypertext for the file if it finds it. HTTP client Browser docking received hypertext parsing, translated into a formatted page, displayed to the user;

(HTTP server impersonation returned)



<6>. After the HTTP server sends the HTTP content, it disconnects the HTTP connection;

<7>. The two sides disconnect the TCP connection;

<span style= "FONT-SIZE:14PX;" >/*------------------------------------------------------------httpserver---------------------------------- -----------------------------------*/#include <winsock2.h> #include <stdio.h> int main (void) {//--------
    --------------//Initialize ... int len = 0;
    Char buff[4096];
    Char msgret[4096];

	
	memset (Msgret, 0x00, sizeof (Msgret));
	----------------------//Initialize Winsock.
	Wsadata Wsadata;
	int iresult = WSAStartup (Makeword (2,2), &wsadata);
		if (iresult! = no_error) {printf ("ERROR at WSAStartup () \ n");
	return 1;
	}//----------------------//Create a SOCKET for listening for//incoming connection requests.
	SOCKET Listensocket;
	Listensocket = socket (af_inet, sock_stream, ipproto_tcp);
		if (Listensocket = = Invalid_socket) {printf ("Error at SOCKET ():%ld\n", WSAGetLastError ());
		WSACleanup ();
	return 1; }//----------------------//The SOCKADDR_IN structure specifies the address Family,//IP address, and port for the socket, which is being bound.
	SOCKADDR_IN Service;
	service.sin_family = af_inet;
	SERVICE.SIN_ADDR.S_ADDR = inet_addr ("10.166.178.225");
	
	Service.sin_port = htons (80);
		if (Bind (Listensocket, (sockaddr*) &service, sizeof (service)) = = Socket_error) {printf ("bind () failed.\n");
		Closesocket (Listensocket);
		WSACleanup ();
	return 1;
	}//----------------------//Listen for incoming connection requests.
		On the created socket if (Listen (listensocket, 1) = = Socket_error) {printf ("ERROR listening on socket.\n");
		Closesocket (Listensocket);
		WSACleanup ();
	return 1;
	}//----------------------//Accept the connection.
	SOCKET AcceptSocket;

	printf ("Waiting for Client to connect...\n");		
		while (1) {acceptsocket = accept (listensocket, NULL, NULL);
			if (AcceptSocket = = Invalid_socket) {printf ("Accept failed:%d\n", WSAGetLastError ());
			Closesocket (Listensocket);
			WSACleanup ();
		return 1;} else {printf ("Client connected.\n");
            memset (Buff, 0x00, sizeof (buff));
            Len = recv (acceptsocket, buff, 4096, 0);

            printf ("msg recv:\n%s\n", buff); sprintf (Msgret, "http/1.1 ok\n" "Date:fri, Geneva 01:59:03 gmt\n" "Server:apache" "Location:http: www.sina.com.cn/\n "cache-control:max-age=60\n" "Expires:fri, Geneva 02:00:03 gmt\n" "Vary:accept-e ncoding\n "" content-length:231\n "" content-type:text/html; Charset=iso-8859-1\n "" X-cache:miss from xd33-82. hp08040021.sina.com.cn\n "" connection:close\n\n "" <! DOCTYPE HTML public \ "-//IETF//DTD HTML 2.0//en\" >\n " 

Server-side Display results ...


2.HTTP Request & Response (response) Message Interpretation (Http Analyzer V5 analysis Results)

2.1 HTTP Request message (header section)


Get request: Is the information of the resource specified by the request URL as an entity, and if the request URL is just a data generation process, then the final response entity is the resource to which the result of the processing is directed, not the description of the processing process. Get is a request to send data to the server.

[1] get/http/1.1

[2] Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave

-flash, Application/x-silverlight,application/vnd.ms-excel, Application/vnd.ms-powerpoint,application/msword, */* A list of content types that the client can identify.

[3] Languages The ACCEPT-LANGUAGE:ZH-CN client can interpret: Simplified Chinese

[4] Ua-cpu:x86 (not shown above)

[5] Accept-encoding:gzip, deflate//client can interpret the type

[6] user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; infopath.3;. NET CLR2.0.50727;. NET CLR 3.0.04506.648;. NET CLR 3.5.21022;. NET 4.0C;. NET4.0E)

client browser model (same as Next)


[7] host:10.166.178.225 Submit Request page (local IP address is tested with local machine, this can be a website domain name such as: www.dlut.edu.cn).

[8] Connection:keep-alive The TCP connection remains open.

2.2 HTTP Response message (header part)


[1] (Status-line) http/1.1 OK

[2] Date:fri, 02Mar 01:59:03 GMT

[3] server:apachelocation:http://www.sina.com.cn

[4] Cache-control:max-age=60

[5] expires:fri,02 Mar 02:00:03 GMT

[6] Vary:accept-encoding

[7] content-type:text/html; Charset=iso-8859-1

[8] X-cache:miss from xd33-82.hp08040021.sina.com.cn

[9] Connectionclose

The response information is also made up of the corresponding 4 parts:

Protocol status Description, http/1.1 represents the protocol version, and a. OK indicates that the server has successfully processed the request made by the client. 200 indicates that the HTTP response code was successful. The HTTP answer code consists of 3 digits, with the first number defining the type of the answer code:

1xx-Information Class (information), which indicates receipt of a Web browser request, is being further processed

2xx-Success Class (successful), which indicates that user requests are received correctly, understood and processed for example: OK

The 3xx-redirect Class (redirection) indicates that the request was unsuccessful and the customer must take further action.

4xx-Client error, which indicates that the client submitted a request with an error such as: 404 Not Found, means that the document referenced in the request does not exist.

5xx-Server error indicates that the server was unable to complete the processing of the request: 500

Response header: As with the request header, it indicates the function of the server and identifies the details of the response data.

Blank line: A blank line that must exist between the response header and the response body, indicating the end of the response header, followed by the response body

Response Body: The Web page content returned by the server.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.