How to Use
Socket
Proceed
HTTP
AccessWe usually access a URL through a browser: After a URL request is submitted, the browser sends the request to the target server or proxy server, and the target server or proxy server returns the data we need, after receiving the data, the browser saves it as a file and displays it. Next, let's take a look at how to use the winsock2.h interface to implement this function? To simplify the problem, make the following assumptions: perform HTTP access through the proxy server, thus eliminating the DNS resolution steps for the URL. Assume that the proxy server address is 192.168.0.1: 808. This function consists of the following parts: 1. How to establish a connection? 2. How to send a request? 3. How to receive data? 4. How can I determine whether data reception is complete? Next, let's look at how to solve these problems in sequence?
I,
How to establish a connection with the serverHTTP is basically TCP, so we need to establish a connection with the server before sending data. To establish a connection, see the following function socket_open:/** open the socket and return socketid.-1 indicates failure */INT socket_open (int ip, int port, int type) {socket socketid; struct sockaddr_in serv_addr; int status; socketid = socket (af_inet, sock_stream, 0); If (INT) socketid <0) {printf ("[Error] Create a socket failed! /N "); Return-1;} memset (& serv_addr, 0, sizeof (serv_addr); serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = ntohl (IP ); serv_addr.sin_port = htons (ushort) Port); status = connect (socketid, (struct sockaddr *) & serv_addr, sizeof (serv_addr); If (status! = 0) {printf ("[Error] connecting failed! /N "); closesocket (socketid); Return-1;} return socketid;} The call method is as follows: int socketid = socket_open (0xc0a80001, 808, 0 ); // 0xc0a80001 is the hexadecimal method of 192.168.0.1.
II,
How to send a requestAdditional protocol header: static const char * protocolhead = "Get http://www.xxx.com/index.html HTTP/1.1/N" "accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord, */N "" Accept-language: ZH-CN/N "" User-Agent: ipanelbrowser/2.0/N "" Host: www.xxx.com: 80/N "" connection: Close/n "here, get is used to obtain the specified ur L. After the connection is established, send the data: Send (socketid, protocolhead, strlen (protocolhead), 0); after sending the HTTP request, wait for receiving the data.
III,
How to receive dataHere we use the select loop query method to determine whether data has arrived: struct timeval TM = {4096}; fd_set fds_r; int status; char recvbuf [] = {'/0 '}; fd_zero (& fds_r); fd_set (socketid, & fds_r); status = select (socketid + 1, & fds_r, 0, 0, & TM ); // socketid here is the largest FD if (status> 0 & fd_isset (socketid, & fds_r) {printf ("socket is readable... FD = [% d]/n ", socketid); Recv (socketid, recvbuf, 4096, 0);} Then the data packet is saved to the buffer.
IV,
How to determine whether data reception is completeFirst, judge the status of the returned data. Only when the status is "HTTP 200 OK" indicates that the returned data is correct. Then, the data is parsed and saved, if the status is HTTP 404 not found or other statuses, the resource is not found or there are other problems. For more information, see
HTTP 1.1 Status Code and its meaning. When the data is returned correctly, in order to separate the actual data from the protocol for storage, the HTTP packet needs to be parsed to get the Content-Length, then, find the first blank line in the current data packet containing Content-Length or the subsequent data packet. This is the starting position of the content (content), and then match the Content-Length obtained by the preceding parsing, then you can know when the data reception is complete. The line break is "/R/N" and is compatible with "/N" or "/R". Set the line break to ^ p, if the blank line is in the middle or end of the content, you can find "^ P". If it is in the beginning, find ^ p. Basically, the above four problems are solved, so the whole problem is solved!