Requirements: Every day, when opening web pages or apps, the information you browse is almost always accessed by sending HTTP or HTTPS request responses. Using C# based on the Hypertext Transfer Protocol and native sockets to send HTTP requests.
agreement
Hypertext Transfer Protocol (HTTP) is a stateless application layer protocol used for distributed, collaborative hypertext information systems.
HTTP / 1.1 Protocol (RFC 9112):The hyperlink login is visible. HTTP Semantics (RFC 9110):The hyperlink login is visible.
Example request:
Sample response:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Accept-Ranges: bytes
Content-Length: 51
Vary: Accept-Encoding
Content-Type: text/plain
Hello World! My content includes a trailing CRLF.
Socket sends requests
The source code is as follows:
The source code supports sending requests to HTTP and HTTPS protocols. As shown below:
Connection:close vs keep-alive
If you use ReadToEndAsync to read all data from the response, you need to set the Connection to close. If you set it to keep-alive, the ReadToEndAsync response will freeze. Reasons for not freezing: Many web servers (such as Nginx, Apache, or certain API gateways) will actively initiate a FIN packet to close the TCP connection if the client is native to write a socket with no subsequent action, or if the server triggers an idle timeout (usually set to 5 seconds).
HTTP pipeline, sending multiple requests simultaneously:The hyperlink login is visible.
Transfer-Encoding: chunked 表示数据是以分块的形式传输的。每个数据块前面都有一个指示该块大小的十六进制数字,这个数字后面跟着一个换行符(CRLF),然后是实际的数据内容,再接着另一个换行符来结束这一块。这种编码方式允许服务器在不知道全部内容长度的情况下开始发送数据。
FDD8 is such a hexadecimal number that specifies the size of the following data block (in bytes). Converting fdd8 from hexadecimal to decimal results in 65,016 bytes. This means that immediately after fdd8 and CRLF are 65,016 bytes of data. After that, there may be more data blocks until a block of size 0 is encountered, indicating the transmission is complete.
FDD8 tells the receiver that the next data block is 65,016 bytes long. |