......
I. Interaction between browsers and servers
Ii. Write your own "mini" Asp.net framework
Iii. Asp.NetRequest and Response Process
1,Before that, let's first simulate what the browser and server did when we went to request a URL:
When we request a website, the browser will encapsulate a request message and send it to the server. The server will receive the browser request message for analysis and processing, generate response messages and process generated html or text images, and return them to the browser as a stream. the browser will parse the data returned by the server, display html, images, js, and css in a browser. Communication between them follows the Http protocol. As the name suggests, HTTP Protocol defines the communication mode of file transmission between the server and the client, and defines the format of a series of request responses, it is called an Http request message (the data format in which the browser sends the request to the server) and an Http Response Message (the data format that the server returns to the browser ).
An Http request Message consists of four parts: request line, request header, empty line, and request data. The following is the general format of the request Message:
Response Message format:
HTTP response is composed of three parts: Status line, message header, and response body.
As shown below, the HTTP Response format is very similar to the request format:
TCP is the underlying communication protocol and defines the specifications of data transmission and connection methods;
HTTP is an application layer protocol and defines the content specification for data transmission;
Therefore, the HTTP protocol also uses the TCP protocol to transmit data through Socket communication.
Next, let's demonstrate how to understand the Http protocol through Socket communication:
Create a winform program, set the Ip address and port, and listen to Http requests when the service is enabled.
btnStart_Click( Socket socket = IPEndPoint ip = socket.Listen( .btnStart.Enabled = Task.Factory.StartNew(o => Socket serverSocket = o ( Socket clientSocket = [] data = [ * len = clientSocket.Receive(data, , data.Length, SocketFlags.None); (len <= strRequest = Encoding.UTF8.GetString(data, [] sendText = Encoding.UTF8.GetBytes( + sendData = .Format( clientSocket.Send(Encoding.UTF8.GetBytes(sendData)); clientSocket.Send(sendText); clientSocket.Shutdown(SocketShutdown.Both); }
In this way, we have prepared a small server that listens to browser requests. When the browser sends a Response Message to the browser, the browser accepts the request message and parses it.
Ncoding. UTF8.GetBytes ("this is the data sent back by the server! \ R \ n this is the packet sent by the browser \ r \ n "+ strRequest );
Byte [] sendText = clientSocket. Send (sendText );
Here, you can also send images and static html files to the response stream in the form of byte arrays. What you need to do is change the Content-Type in the Response Message
"Image/png", or text/html; charset = UTF-8 type.
---------------------------------------------
Next we will introduce how browsers interact with servers in practical applications.
IIS6 introduction:
HTTP. SYS: a network driver that is always running and is responsible for listening to http requests.
W3SVC: creates, recycles, and monitors workflows.
Metabase: stores configuration information.
Aspnet_isapi.dll: CLR loading, application domain creation, and web application initialization.
The browser encapsulates request packets to IIS -->
IIS listens to browser requests -->
IIS distributes requests to the W3SVC service -->
W3SVC obtains the working process of the target application based on Metabase --> -- if it is a static file, the original path is returned.
Working Process W3WP. EXE uses aspnet_isapi.dll to load. newFramework to Process Dynamic Resources
At this point, if IIS finds a static resource, it will return directly, then we can simply modify the simulated server program. So isn't it about dynamic programs!
Request. QeuryString [], Response. Write (), use html templates, expand HttpModule, and create general processing programs. What should I do !?
In the next article, we will simulate the ASP. NET pipeline to process dynamic resource files.