In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to parse the HTTP protocol in C# network programming. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
After we use Asp.net technology to develop Web applications, when users enter a URL in the browser, they send a HTTP request to the server. At this time, we use the HTTP protocol of the application layer. In the last topic, we briefly introduced the knowledge of the network protocol, mainly to pave the way for the later HTTP protocol. Only we have a clear understanding of the HTTP protocol. In this way, when we use Asp.net technology to develop Web applications, we can think more about our applications in terms of network protocols, rather than just dragging the use of server controls, which can also help us develop our own custom web server.
At the same time, I would like to share with you my understanding of the nature of Asp.net. If there is anything wrong, please point out that, first of all, when we design an algorithm, we should specify the input parameters and the return of the algorithm (the algorithm is also a handler). In fact, the web web page developed by Asp.net can be understood as a handler. Because what we see in web browsers are HTML documents (HTML, that is, the output of the program after Asp.net web page processing, that is, the return of the algorithm), but the input parameters are a Http request (a URI address of the request) entered by the user through the browser. Asp.net technology helps us to translate the requested aspx page into an HTML document, and then the HTML document sends the HTML document to the browser through the HTTP protocol. The browser then parses the tag (the HTML document is just a string, and if there is no browser parsing, we will see some strings, rather than a visual interface) into a visual interface. Such a web request ends. Later, we will share with you some of the objects behind what we have done for us in Asp.net. Let's go back to the introduction of the Http protocol.
1. Brief introduction of HTTP protocol
HTTP Chinese is the hypertext transfer protocol, which is easy to understand from the name. Http protocol is the browser that transfers hypertext markup language documents (that is, Html documents) from the web service to the client. It belongs to an application layer protocol.
Second, the working process of the network
When users want to visit a web page in the network, they roughly go through the following steps:
1. Users should first determine the URL where the web page file is located (uniform resource locator, that is, the home address of the web page on the network, through which you can find the web page) such as www.cnblogs.com
two。 The browser sends a request to DNS (domain name server) and tells DNS, "I want to convert www.cnblogs.com to the IP address it defines". Here, we can simply understand DNS as a dictionary. If you know the domain name, you can know the IP address of the domain name. They have this mapping relationship.
After receiving the request, 3.DNS starts the query and returns the result to the browser when it is found. If the domain name is www.cnblogs.com, the corresponding IP address is 61.155.169.116.
4. After knowing the IP address, the browser sends a TCP connection request with port number 80 to the host with IP address 61.155.169.116 (the HTTP protocol is based on the transport layer TCP). Port 80 is the default port for the server to provide web services.
5. After the connection is established, the browser issues a HTTP request, such as GET http://www.cnblogs.com/ HTTP/1.1
6. When the server with the domain name www.cnblogs.com receives the request, it sends a html file to the browser
7. After the file is sent, the server actively closes the TCP connection.
8. The browser receives the transmitted page and displays
9. If the Html file contains pictures, establish a TCP connection with the server again so that the pictures can be downloaded
In the steps described above, after the browser makes a request, how do you download the HTML document from a server to the host requesting the web page? This process is completed by HTTP, that is, the transfer of hypertext files, and the HTTP protocol is the foundation of the web server.
II. HTTP request
A Http request consists of three parts: the request line, the request header and the request data. The format of an HTTP request is generally as follows:
Request method URL HTTP version number
Request header information
Request data
The method of HTTP request is shown in the following table:
Get and Post methods are often used. When a request is made using the Get method, the request data is empty, so the HTTP request line consists of two parts: the request line and the request header information. Let's take a look at the specific HttP example:
When you enter "www.cnblogs.com" in the address bar of the browser, we will issue a HTTP request, specifically:
And it can be seen from the picture that when the web page contains files such as picture scripts, the client will continue to make a request with the server for the required pictures and script files.
Add: after a friend's message, I would like to add here that only one TCP connection is usually established, and the Connetion field of the HTPP request header indicates that when the server receives a request with Connection: Keep-Alive, it will also add the same field to the response header to use Keep-Alive. In this way, the HTTP connection between the client and the server is maintained and will not be disconnected, except in some special cases, when the client sends another request, the established connection is used.
The following is the information about the request header:
Accept: represents the type of data received by the client. For example, Accept:text/html means that the client can receive text of type HTML
User Agent: indicates the type of client software
Referer: represents the url of the previous connection, such as url that jumps to the previous page of this page.
The image above shows the process of downloading a HTML file to the browser of this example through the Get method. When we log in to the blog garden home page and enter the user name and password and click the confirm button, we send the HTTP request through the Post method. Here is a screenshot:
As you can see from the figure, there is a blank line in the HTTP request made through the Post method (the blank line is followed by the request data), but not in the request made by the Get method.
3. HTTP response
Similarly, the Http response consists of three parts: the status line, the response header and the response data. The Http response format is as follows:
Status line
Response head
Response data
The status line starts with the HTTP version number, followed by a number of 3 to represent the response code, which is used to tell the client whether the server has produced the expected response. Such as HTTP/1.1 200OK.
Five response codes are defined in HTTP/1.1:
1xx: indication message-indicates that the request has been received and continues processing
2xx: successful-indicates that the request has been successfully received, understood and accepted
3xx: redirect-- further operations must be taken to complete the request
4xx: client error-request has syntax error or request cannot be implemented
5xx: server side error-the server failed to fulfill the legitimate request
The specific response code is described below:
The HTTP response header is used by the server to provide request document information or server status information to the client, as shown in the figure
IV. Summary
At the end of this article, HTTP is only one of the protocols in the application layer, and there are other protocols in the application layer, such as FTP (File transfer Protocol), SMTP (e-mail protocol) and so on. The latter project intends to apply the HTTP protocol only to customize a simple Web server to simulate our usual process of sending a Http request and a response from the server after entering the URL in the browser.
On how to parse the HTTP protocol in C# network programming is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.