Implementing breakpoint Continuation with Visual C #

Source: Internet
Author: User
Tags http request

Before understanding the principle of HTTP breakpoint continuation, let's take a look at the HTTP protocol, the HTTP protocol is a simple protocol based on TCP, which is divided into two kinds: request and reply. A request protocol is a protocol that sends a message when a client (browser) submits a request to the server (WEB server). A reply protocol is a protocol that the server (Web server) responds to a message to a client (browser). Both the request and reply protocols are composed of a header and a body. The head and body are separated by a line of empty behavior.

The following is an example of a request message and a corresponding reply message:

GET /image/index_r4_c1.jpg HTTP/1.1
Accept: */*
Referer: http://192.168.3.120:8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)
Host: 192.168.3.120:8080
Connection: Keep-Alive
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 24 Jun 2003 05:39:40 GMT
Content-Type: image/jpeg
Accept-Ranges: bytes
Last-Modified: Thu, 23 May 2002 03:05:40 GMT
ETag: "bec48eb862c21:934"
Content-Length: 2827
….

Here we say "break-through", as the name suggests, the continuation of the breakpoint is in the last download when the disconnect began to continue downloading.

In the HTTP protocol, you can include a range segment in the request message header to indicate where the client wants to continue downloading.

For example, starting from the 1024th byte download, the request message is as follows:

GET /image/index_r4_c1.jpg HTTP/1.1
Accept: */*
Referer: http://192.168.3.120:8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)
Host: 192.168.3.120:8080
Range:bytes=1024-
Connection: Keep-Alive

. Related classes in net

Understanding the principle above, let's look at what classes in the. NET framework are available for us to do.

Complete HTTP request

System.Net.HttpWebRequest

The HttpWebRequest class provides support for the properties and methods defined in WebRequest, as well as additional properties and methods that enable users to interact directly with servers that use HTTP.

HttpWebRequest exposes public HTTP header values sent to Internet resources as properties, by method or system settings. The following table contains the complete list. You can set other headers in the Headers property to name/value pairs. Note, however, that some public headers are considered restricted and either are exposed directly by the API or are protected by the system and cannot be changed. Range also belongs to the protected list, however,. NET provides a more convenient operation for developers, the AddRange method, which adds a range of bytes from the beginning or end of the request data to the request header

Complete file access

System.IO.FileStream

The FileStream object supports random access to a file using the Seek method, which allows the read/write location to be moved to any location in the file. This is done through the byte offset reference point parameter. The byte offset is relative to the lookup reference point, which can be the start, current position, or end of the underlying file, represented by the three properties of the SeekOrigin class, respectively.

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.