HTTP status codes 206 and 416, status 206 and 416

Source: Internet
Author: User

HTTP status codes 206 and 416, status 206 and 416

The status code table in the HTTP 2xx range shows: "The request sent by the client has been accepted by the server and processed successfully ".

TTP/1.1 200 OK is the standard response after successful HTTP requests

HTTP/1.1 206 status code indicates: "The client captures part of the resource by sending the Range Request Header Range", which is generally used

  • Solve the Problem of downloading large files

  • Solve CDN and original HTTP server problems

  • Use tools such as lftp, wget, and telnet to test the resumable Data Transfer

1. How can I determine whether a remote server supports HTTP 206?
root@iZ25n2yx37sZ:~# curl -I http://static.cnblogs.com/images/icon_form.gifHTTP/1.1 200 OKServer: TengineContent-Type: image/gifContent-Length: 913Connection: keep-aliveDate: Mon, 22 Aug 2016 03:30:54 GMTCache-Control: public,max-age=86400Accept-Ranges: bytesETag: "6821606d29bce1:0"Last-Modified: Fri, 15 Feb 2013 03:06:19 GMTVia: cache22.l2cm9-1[0,304-0,H], cache48.l2cm9-1[0,0], kunlun9.cn3[0,200-0,H], kunlun8.cn3[0,0]Age: 38765X-Cache: HIT TCP_MEM_HIT dirn:11:775286164X-Swift-SaveTime: Mon, 22 Aug 2016 03:34:29 GMTX-Swift-CacheTime: 86400Timing-Allow-Origin: *EagleId: d38a7a8814718754198854631e

There are two request headers that we are concerned about:

Accept-Ranges: bytes-ThisThe response header indicates that the server supports Range requests.And the Unit supported by the server is byte (this is also the only available unit ). we can also know that the server supports resumable upload and multiple parts of the file to be downloaded at the same time. That is to say, the download tool can use a range request to accelerate the download of this file.Accept-Ranges:NoneThe response header indicates that the server does not support range requests.

Content-Length: 913The Content-Length response header indicates the size of the response object, that is, the actual image file size is913 bytes.

2. How to send a range Request Header

Now that you know that the server where the image is located supports Range requests, you need to send a GET request containing the Range request header:

Range: bytes=0-1024

The complete request data should be like this. The first line is:

GET /images/misc/static/2012/11/ifdata-welcome-0.png HTTP/1.1 

Then, you need to send a Host request header to specify the Host and port number of the requested resource:

Host: static.cnblogs.com

Finally, the Range request header is sent, specifying the desired byte Range:

Range: bytes=0-1024 
A. Use the telnet command

The telnet command allows you to use the Telnet protocol to communicate with remote hosts (servers. all Unix-like operating systems and MS-Windows systems contain Telnet clients. start the Telnet client and enter the Telnet prompt. Run the following command:

telnet your-server-name-here www
telnet your-server-name-here 80

To connect to the remote server static.cnblogs.com through port 80, enter:

telnet static.cnblogs.com 80 

Output result:

root@iZ25n2yx37sZ:~# telnet static.cnblogs.com 80Trying 211.138.122.237...Connected to static.cnblogs.com.w.alikunlun.com.Escape character is '^]'.GET /images/icon_form.gif HTTP/1.1Host: static.cnblogs.comRange: bytes=0-1024

In this example, the range request (0-1024 bytes) is used to request the/images/icon_form.gif file on static.cnblogs.com.

B. Use the curl command

The curl command is a tool used to exchange data with the remote server. It supports HTTP/FTPSFTP/fileresolution. In the following example, two remote file ifdata-welcome-0.png is used, and then the cat command is used to merge the two data segments into the complete file:

root@iZ25n2yx37sZ:~# curl -v -s  --header "Range: bytes=0-2000" http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part1* About to connect() to images2015.cnblogs.com port 80 (#0)*   Trying 106.2.189.18... connected> GET /news/24442/201608/24442-20160805112458981-1554012564.jpg HTTP/1.1> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3> Host: images2015.cnblogs.com> Accept: */*> Range: bytes=0-2000> < HTTP/1.1 206 Partial Content< Server: marco/0.17< Date: Mon, 22 Aug 2016 14:31:15 GMT< Content-Type: image/jpeg< Content-Length: 2001< Connection: keep-alive< X-Request-Id: ebeb021b9075223913e28033da978ce5; 6402d8710dc2ba320b03fc7663555a1d< X-Source: U/200< ETag: "a7efeb9fca51518501be9e16d064c366"< Last-Modified: Fri, 05 Aug 2016 03:26:04 GMT< Expires: Fri, 26 Aug 2016 23:05:26 GMT< Cache-Control: max-age=656155< Accept-Ranges: bytes< Age: 279706< X-Cache: MISS(S) from mix-bj-pek-106; HIT(R) from ctn-bj-pek2-027< Content-Range: bytes 0-2000/95164< Via: T.77102.S.1, T.088.M.1, T.088.M.2, V.cache_img_92, S.mix-bj-pek-108, V.mix-bj-pek-106, T.18922.R.1, M.ctn-bj-pek2-027< { [data not shown]* Connection #0 to host images2015.cnblogs.com left intact* Closing connection #0

Part 2 and merge

curl  --header "Range: bytes=0-2000" http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part1curl  --header "Range: bytes=2001-" http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part2cat part1 part2 >> test1.png

You can also use the-r option (you can also add the-v option to view the request header and Response Header ):

curl -r 0-2000 http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part1curl -r 2001-" http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part2cat part1 part2 >> test1.png
3. How do I enable the Accept-Ranges response header?

Most web servers support byte range requests. Apache 2.x users can try mod_headers in httpd. conf:

Header set Accept-Ranges bytes

The Lighttpd user tries to configure the following in lighttpd. conf:

## enabled for all file types ##server.range-requests = "enable"## But, disable it for pdf files ##$HTTP["url"] =~ "\.pdf$" {    server.range-requests = "disable"}
4. HTTP 416 errors and resumable Data Transfer

Let's take a look at what an HTTP 416 error represents?

The Requested Range cannot be met (Requested Range not satisfiable)

 

I cannot see it clearly, because I have never met.

# After exploring and asking about the client, I found that HttpURLConnection was used for download. So I googled and got some key information:

HTTP response code: 416 is caused by incorrect Range set during file reading. Specifically, the following line of code is incorrect: httpConnection. setRequestProperty ("RANGE", "bytes = 1024-"); this RANGE obviously cannot exceed the file size

The RANGE set by the client is the file size.

Imagine how to know the file size if the file exists on a remote server?

You must initiate at least two requests. For the first request, you do not need to download the entire file. You only need to get the Content-Length Size of Response. For the second request, write the Content-Length value into RANGE to download the file.

root@iZ25n2yx37sZ:~$ curl -v -s  --header "Range: bytes=300000-" http://images2015.cnblogs.com/news/24442/201608/24442-20160805112458981-1554012564.jpg -o part2* About to connect() to images2015.cnblogs.com port 80 (#0)*   Trying 106.2.189.18... connected> GET /news/24442/201608/24442-20160805112458981-1554012564.jpg HTTP/1.1> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3> Host: images2015.cnblogs.com> Accept: */*> Range: bytes=300000-> < HTTP/1.1 416 Requested Range Not Satisfiable< Server: marco/0.17< Date: Mon, 22 Aug 2016 14:44:03 GMT< Content-Type: text/html; charset=utf-8< Content-Length: 207< Connection: keep-alive< Cache-Control: no-store< Content-Language: en< Via: T.18922.R.1, M.ctn-bj-pek2-026< X-Cache: HIT(R) from ctn-bj-pek2-026< X-Request-Id: e6817ee753535f32b25e7651400269b7< { [data not shown]* Connection #0 to host images2015.cnblogs.com left intact* Closing connection #0

The cause of the error code 416 is that the Set Range is incorrect. The solution is also very simple. Remove the Range in the first request.

// After deletion, the whole world will be clean! Conn. setRequestProperty ("Range", "bytes =" + startPosition); // startPosition = 0

 

The discussion is about CDN addresses. Does CDN support resumable data transfer?

On the contrary, 416 indicates that resumable data transfer is supported. After the server obtains a Range, it needs to check its values, including:

Start position non-negative

The end position must be greater than the start position.

The start position must be less than the file length minus one (because the index here starts from 0)

If the end position is greater than the file length minus one, you need to set its value to the file length minus one.

  

References

Http://spetacular.github.io/2015/01/30/http-code-416-and-download.html
Http://www.checkupdown.com/status/E416_cn.html
Http://blog.csdn.net/zollty/article/details/9176829
Http://www.pureweber.com/article/resumable-download
Http://emacsist.github.io/2015/12/29/Http-%E5%8D%8F%E8% AE % AE %E4%B8%AD%E7%9A%84Range%E8%AF%B7%E6%B1%82%E5%A4%B4%E4%BE%8B%E5%AD%90/
Http://blog.csdn.net/zollty/article/details/9176829/

 

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.