Preface
First in the article said before, save everyone find I have no such hair, duang a bunch of people scold me.
The title of this article is a bit big, actually quite low, I need to explain at the beginning. I just want to write a small console that is designed to emulate the browser's HTTP communication.
Not really able to use the browser.
To put it simply, the whole work of today is
Set up socket→ send request message → server return message → display message. That's it.
To tell the truth, isn't it a little bit low?
My purpose is only two. 1. Deepen your understanding of the HTTP protocol 2. Deepen the understanding of B/s structure.
If you already know these things very well. Then don't talk to me, let me entertain myself for a while.
Theoretical Support
1.http Working principle
The previous article introduced, the Operation mechanism of HTTP protocol, simply say is such a picture.
2. How the browser works
Based on this operating mechanism, I guess the browser should work like this.
Of course, in this schematic, I deliberately ignored the browser's ability to interpret JavaScript, cache management capabilities, and so on ...
The main reason is that it is too complicated to take into account the diagram. Deviate from our research HTTP protocol, understand the original intention of B/s mode.
Of course, the root cause is, I will not! (All of a sudden, so sad ...) No, I'll cry first.
Code
1. Ideas
The idea is nothing to say is the above picture.
Set up socket→ send request message → server return message → display message.
2. Code
1 /*2 This program writes the host address to death.3 If you want to be more like, you can add an input. The response message is then obtained based on the URL entered. 4 */5 6Socket socket =NewSocket (SocketType.Stream, protocoltype.tcp);7 //connect to the server. The default port number for the HTTP protocol is 80. Each server software listens to a port (other software can not listen to this port), the data sent to this port will only be received by this server software. 8Socket. Connect (NewDnsendpoint ("127.0.0.1",8080));9 //stream of Read and write socket communication dataTen using(NetworkStream NetStream =NewNetworkStream (socket)) One using(StreamWriter writer =NewStreamWriter (netStream)) A { - //here is to write the request message before, steal a little lazy, not much write -Writer. WriteLine ("get/index.html http/1.1"); theWriter. WriteLine ("host:127.0.0.1:8080"); - //Empty line carriage return, indicating end of instruction - //Do you remember the format of the HTTP request message? Between the message header and the newspaper style is separated by a blank line carriage return. - writer. WriteLine (); + } - //start reading . + using(NetworkStream NetStream =NewNetworkStream (socket)) A using(StreamReader reader =NewStreamReader (netStream)) at { - stringLine ; - while(line = reader.) ReadLine ())! =NULL) - { - //read one line output line - Console.WriteLine (line); in } - } to //several release resources +Socket. Disconnect (false);
Debug
1. Expected effect
Yes, this is the response message of the browser requesting the IIS Welcome page. If our console can output the same response message, it proves that the experiment was successful!!
2. Debug results
The response message is displayed correctly. It worked!
Testimonials from the end of the experiment
Finish this thing, seems to have no sense of accomplishment ha!
But this program let me know more about the HTTP protocol, and B/s structure.
You see, the browser is sending a request message, and the server returns the response message based on the request message. The basic principle of the browser/server is this.
Request/Response, no response is requested. (Of course, we can talk about the message push, but that's something!) )
Write a simple browser, ASP. NET Core Knowledge (3)