一、HTTP原理
1.HTTP工作原理(实例分析):
<1> http客户端与http服务器建立TCP连接;
<2> http客户端通过浏览器向http服务器80端口发送HTTP连接请求;
<3>如果http服务器允许访问,则返回连接确认,双方建立HTTP连接;
<4>. http客户端发送HTTP页面请求(Http Analyzer V5分析结果)
<5>. http服务器寻找该HTTP文件,如果找到则返回该文件的超文本。http客户端的浏览器对接收到的超文本进行解析,翻译成格式化的页面,显示给用户;
(Http服务器模拟返回)
<6>. http服务器发送完HTTP内容后,断开HTTP连接;
<7>. 双方断开TCP连接;
- <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 that 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 200 OK\n"
- "Date: Fri, 02 Mar 2012 01:59:03 GMT\n"
- "Server: Apache"
- "Location: http://www.sina.com.cn/\n"
- "Cache-Control: max-age=60\n"
- "Expires: Fri, 02 Mar 2012 02:00:03 GMT\n"
- "Vary: Accept-Encoding\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"
- "<html><head>\n"
- "<title>title test</title>\n"
- "</head><body>\n"
- "<h1>test body</h1>\n"
- "<h2>test sub body</h2>\n"
- "<p>My First Test!</p>\n"
- "</body></html>" );
- len = send( AcceptSocket, msgret, sizeof(msgret), 0 );
- if( len <= 0 )
- {
- printf( "send failed\n" );
- }
- }//end else
- }//end while
- // No longer need server socket
- closesocket(ListenSocket);
- WSACleanup();
- return 0;
- }</span>
服务器端的显示结果…
2.HTTP请求(request)&响应(response)报文解读(Http Analyzer V5分析结果)
2.1 HTTP请求报文(Header部分)
GET请求:是以实体的方式得到由请求URL所指定资源的信息,如果请求URL只是一个数据产生过程,那么最终要在响应实体中的是处理过程的结果所指向的资源,而不是处理过程的描述。GET是向服务器发索取数据的一种请求。
[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, */* //客户端可识别的内容类型列表。
[3]Accept-Language:zh-cn 客户端所能解释的语言:简体中文
[4]UA-CPU: x86(上面未体现出来)
[5]Accept-Encoding:gzip, deflate //客户端可以解释的类型
[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)
//客户端浏览器型号(同下)
[7]Host: 10.166.178.225提交请求页面(用本地机器测试,即为本地IP地址;此处可以是网站域名如:www.dlut.edu.cn)。
[8]Connection:Keep-Alive TCP连接保持打开。
2.2 HTTP响应报文(Header部分)
[1](Status-Line)HTTP/1.1 200 OK
[2]Date: Fri, 02Mar 2012 01:59:03 GMT
[3]Server:ApacheLocation:http://www.sina.com.cn
[4]Cache-Control:max-age=60
[5]Expires: Fri,02 Mar 2012 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
该响应信息也以对应的4部分组成:
协议状态描述,HTTP/1.1表示协议版本,200 OK表示服务器已经成功处理了客户端发出的请求。200表示HTTP的应答码成功。HTTP应答码由3位数字构成,其中首位数字定义了应答码的类型:
1XX-信息类(Information),表示收到Web浏览器请求,正在进一步的处理中
2XX-成功类(Successful),表示用户请求被正确接收,理解和处理例如:200 OK
3XX-重定向类(Redirection),表示请求没有成功,客户必须采取进一步的动作。
4XX-客户端错误(Client Error),表示客户端提交的请求有错误 例如:404 NOT Found,意味着请求中所引用的文档不存在。
5XX-服务器错误(Server Error)表示服务器不能完成对请求的处理:如 500
响应报头:跟请求报头一样,它指出服务器的功能,标识出响应数据的细节。
空行:也是属于响应报头和响应正文之间必须存在的一个空行,表示响应报头结束,接下来是响应正文
响应正文:也就是服务器返回的网页内容。