Socket Request Web Server procedure

Source: Internet
Author: User

At first we need to understand one thing, because this is the premise of this article:

The HTTP protocol is just an application-layer protocol that transmits data over TCP at the bottom. Therefore, the process of a browser accessing a Web server must first have a "connection established" occurrence.

One might ask: it is well known that the HTTP protocol has two main features, one is "no connection" and the other is "stateless". Does the "no connection" here conflict with the above statement? In fact, there is no contradiction, but people's understanding of the word "connection" is different. First, let's look at the process by which the browser makes an HTTP request to the Web server and the Web server replies to the browser:

1) The browser creates a socket and connects to the server by the given IP (domain name) and port (default = 80). such as the use of similar socket.connect (), Socket.beginconnect () and other methods;

2) After the connection is successful, the browser sends the request data to the Web server according to the HTTP protocol specification (about the protocol, as described later). such as "Request Line", "Request Header" and "Request data", and so on, this may use similar socket.send (), Socket.beginsend () and other methods. "For request lines, request headers, etc. in the HTTP protocol, see http://www.cnblogs.com/riky/archive/2007/04/09/705848.html"

3) The browser waits for the server to process and return data;

4) The Web server uses Socket.accept (), socket.beginaccept () and other methods to hear the browser's connection and start receiving data sent by the browser. After receiving the request data, parsing the data according to the HTTP protocol specification, then processing, and finally sending the processing results (such as HTML documents) back to the browser, here may use similar socket.send (), Socket.beginsend () and other methods;

5) After the Web server sends out the processing result, closes the socket;

6) The browser receives the data sent back by the Web server (such as HTML) and displays it in the browser UI interface. Close the socket;

7) A "browser to Web server" HTTP request ended;

8) The next time the browser needs to request a Web server, jump to the 1th step loop to start.

Use the diagram to represent the above process:

Figure 1

As shown in 1. Before the browser sends an HTTP request to the Web server, the connection needs to be established first. Yes, the process of establishing a connection between them is similar to what we normally develop the socket program. It follows that the"no Connection" feature of the HTTP protocol does not mean that a connection is not required when the browser is exchanging data with the Web server. So what does the "no connection" feature really mean? Let's look at figure 1. It is found that each time the browser is completed, the server is in a "disconnected" state, and the next time the request is re-established with the server. The non-connected nature of HTTP means that each request of the browser must be re-established with the server, and normally the browser will not maintain a long connection with the Web server. The two features of the HTTP protocol are now summed up as follows:

No connection:

A connection between the server and the browser processes only one HTTP request, and the connection is disconnected after the request processing has ended. Re-establish the connection the next time you request it.

No Status:

The server does not save browser information. That is, on the server side, the results of the first HTTP request processing are not persisted to the second request. If the second request is processed, the result of the first request processing needs to be used, and the browser, on the second request, must re-pass the first processing result back to the Web server (for example, using a cookie).

About "agreement":

This topic is a little big, I can not control it. But for today's article, I will do my best to say a little. A wide range of protocols in the computer, there are countless protocols in the network communication, each layer in the OSI seven layer is a variety of protocols. So what is the nature of the agreement? In the communication protocol, the nature of the agreement is actually a data structure, similar to the structure of the code, said the bottom point, is a byte stream, the first byte is defined as what, the second byte represents what and so on.

The role of the agreement is similar to what we usually call "contract", "agreement", a team cooperation task, the cooperation parties must abide by the prior agreement, the final work can proceed normally. Network communication is also the same, the communication between the two parties to receive/Send data must follow the implementation of the structure to send/receive, one party does not comply with the specification, communication can not be successful. The structure specification here is actually "agreement". The protocol has the following effects:

1) Since is the norm, then according to the standard work, oneself does the others easier understanding, facilitates the exchange;

2) write the specification as a document and provide it to other people to facilitate later extension of others. As long as you know the communication specifications, it is easy to write out the extension module and the original system coordination work.

3) in the computer network communication, some factors determine that we must send and receive data in accordance with the specified format, such as TCP communication, because the data is transmitted in accordance with the "stream", if we do not define the data transmission specification, it is difficult to determine the data boundary of TCP transmission.

In terms of network communication protocol, the application layer protocol is most closely related to our program development (at least for us to use C #, Java), the other to TCP, UDP and other transport layer protocol is almost useless. The communication programs we develop must comply with application-layer protocols that are well-defined, such as browsers and Web servers that adhere to the HTTP application-layer protocol so that they can interact properly. If we develop a program to properly comply with the HTTP protocol, then our program can be like chrome, IE and other browsers to access the Web server.

At the end of the article there is a demo that uses the socket emulation browser to request a Web server, so we can use a type like WebClient, WebRequest and so on to implement it. The demo function is as follows:

1) Use the socket to connect to the Web server (optional);

2) Send HTTP requests in the HTTP protocol format (using the Socket.send method);

3) Parse the data returned by the Web server in the HTTP protocol format (it is actually displayed in the UI interface)

(Developing such a program requires us to be fully familiar with socket programming, HTTP protocol format)

The following is the code that sends the HTTP request:

1  /// <summary>2         ///Send Request3         /// </summary>4         /// <param name= "socket" ></param>5         Private voidSendRequest (socket socket)6         {7             stringH1 ="GET"+ _path +"http/1.1\r\n";8             stringH2 ="Accept: */*\r\n";9             stringH3 ="accept-language:zh-cn\r\n";Ten             stringH4 ="Host:"+ _host +"\ r \ n"; One             stringH5 ="user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.116 safari/537.36\r \ n"; A             stringH7 ="connection:close\r\n\r\n"; -  -             byte[] Send_buffer = Encoding.UTF8.GetBytes (H1 + H2 + h3 + h4 + h5 +H7); the socket. Send (send_buffer); -Print ("request complete, wait for Web server reply ..."); -Socket. BeginReceive (_buffer,0,640*1024x768, Socketflags.none,NewAsyncCallback (onreceive), socket); -}
View Code

Here are the following:

Figure 2

SOURCE Download: Http://files.cnblogs.com/xiaozhi_5638/socket_browser.rar

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.