Implement HTTP protocol directly with socket (download exclusive)

Source: Internet
Author: User

There are many ways to download a file from an HTTP server, and "enthusiastic" Microsoft offers a WinInet class that is handy to use. Of course, we can also implement these functions ourselves, through the format of the request header can easily achieve the extension of the breakpoint and check the update and so on. In the project included in this article, there is a DLL that supports the HTTP1.1 protocol, which implements the download function directly with the Socket, and implements the following functions:

Connecting hosts

Format Request Headers

Set receive, send timeout

Receive and analyze response headers

Connect, send, set timeout, receive data etc I will not elaborate, Windows socket is already done, call the corresponding function on OK.

To download a file from the server, first send a request to the server. The HTTP request header consists of several line strings. The following is an example of the HTTP request header format. Suppose you want to download the http://www.sina.com.cn/index.html page, the request header is written as follows:

Line 1th: method, requested content, version of HTTP protocol

Download can generally use Get method, the content of the request is "/index.html", the version of the HTTP protocol refers to the version of the browser support, for downloading software does not matter, so with the 1.1 version of "http/1.1";

"Get/index.html http/1.1"

Line 2nd: Host name, Format "host: Hosts"

In this example: "Host:www.sina.com.cn"

Line 3rd: Accepted data types, download the software of course to receive all the data types, so:

"Accept:*/*"

Line 4th: Specifies the type of browser

Some servers will increase or decrease some content depending on the type of customer server, and in this case you can write:

“User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)”

Line 5th: Connection settings

Set to remain connected: "Connection:keep-alive"

Line 6th: To implement a breakpoint continuation, specify where to receive the data, in the following format:

“Range: bytes=起始位置 - 终止位置”

For example, to read the first 500 bytes can be written like this: "range:bytes=0-499", starting from the 1000th byte download:

“Range: bytes=999 -”

Finally, do not forget to add a line of blank lines, indicating the end of the request header. The entire request header is as follows:

GET /index.html HTTP/1.1
Host:www.sina.com.cn
Accept:*/*
User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Connection:Keep-Alive

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.