HTTP Status Codes 206 and 416

Source: Internet
Author: User
Tags ranges response code

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

ttp/1.1 OK is the standard response after the HTTP request succeeds

The http/1.1 206 status code indicates that the client fetches part of the data by sending a range request header range, which is typically used to

    • Solve large file download problems

    • Troubleshoot CDN and raw HTTP Server issues

    • Use tools such as lftp,wget,telnet test for power-down continuation

1. How to tell if the remote server supports HTTP 206
[Email protected]:~# curl-i http://static.cnblogs.com/images/icon_form.gifHTTP/1.1 Okserver:tenginecontent-type : Image/gifcontent-length:913connection:keep-alivedate:mon, 03:30:54 gmtcache-control:public,max-age= 86400accept-ranges:bytesetag: "6821606d29bce1:0" Last-modified:fri, 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, 03:34:29 gmtx-swift-cachetime:86400timing-allow-origin: *EagleId: d38a7a8814718754198854631e

There are two request headers that we are more concerned about:

Accept-Ranges: bytes-This 响应头表明服务器支持Range请求 , and the units supported by the server are bytes (This is also the only available unit). We also know that the server supports the continuation of breakpoints and supports simultaneous downloading of multiple parts of a file, meaning that the download tool can use the range request to speed up the download of the file. Accept-Ranges: none The response header indicates that the server does not support range requests.

Content-Length: 913Content-Length响应头表明了响应实体的大小,也就是真实的图片文件的大小是913 bytes.

2. How to send a range request header

Now that you know that the server that contains the image supports the range request, you need to send a GET request with a range request header:

range:bytes=0-1024

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

You then need to send the host request header to specify the hosts and port numbers on which the requested resource resides:

Host:static.cnblogs.com

Finally, the range request header to be sent specifies the byte ranges you want:

A. Using the Telnet command

The Telnet command allows you to use the Telnet protocol to communicate with a remote host (server). All UNIX-like operating systems and Ms-windows contain Telnet clients. Start the Telnet client and go to the Telnet prompt to execute the command:

Telnet Your-server-name-here www
Telnet Your-server-name-here 80

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

The output is:

[Email protected]:~# 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, use a range request (0-1024 bytes) To request a/images/icon_form.gif file on static.cnblogs.com

B. Using the Curl command

The Curl command is a tool for exchanging data with a remote server. It supports range requests on the Http/ftpsftp/file protocol, in the following example, using a two-segment range to request a remote file ifdata-welcome-0.png, Then use the Cat command to merge two pieces of data into the full file:

[email protected]:~# 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 (#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, 14:31:15 gmt< content-type:image/jpeg< content-length:2001< Connection:keep-aliv e< X-request-id:ebeb021b9075223913e28033da978ce5; 6402d8710dc2ba320b03fc7663555a1d< x-source:u/200< ETag: "a7efeb9fca51518501be9e16d064c366" < Last-Modified : Fri, 03:26:04 gmt< Expires:fri, 23:05:26 gmt< cache-control:max-age=656155< Accept-ra Nges: bytes< age:279706< X-cache:miss (S) from mix-bj-pek-106; Hits (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 I Mages2015.cnblogs.com left intact* Closing connection #0

Part II and merger

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 the 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 to open the accept-ranges response head?

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

Header set Accept-ranges bytes

The LIGHTTPD user attempts to configure the following in lighttpd.conf:

# # Enabled for all file types # #server. range-requests = "Enable" # # and, disable it for PDF files # # $HTTP ["url"] =~ "\.pdf$ "{    server.range-requests =" Disable "}
4. HTTP 416 Error and breakpoint continuation

Let's see what the HTTP 416 error represents?

所请求的范围无法满足 (Requested Range not satisfiable)

 

Look at the unknown, because I have never met.

# #探索 asked the next client's classmate, found that the download is using HttpURLConnection, so Google a bit, get some key information:

HTTP response code: 416是由于读取文件时设置的Range有误造成的,具体的说就是下面这行代码有误:httpConnection.setRequestProperty("RANGE", "bytes=1024-");这个RANGE显然不能超出文件的size

The range set by the client is the file size.

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

At least two requests are to be initiated. The first request, does not need to download the entire file, only need to obtain the response content-length size; the second request, writes the Content-length value in the range, implements the download.

[email protected]:~$ 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 (#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, 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 reason for the return code 416 is that the range set is incorrect. The solution is also simple, removing the range from the first request.

After the deletion, the whole world is pure! Conn.setrequestproperty ("Range", "bytes=" + startposition);//Startposition=0

The discussion is the CDN address, the CDN does not support the continuation of the breakpoint?

On the contrary, 416 is the flag that supports the continuation of a breakpoint. After the server gets a range, it needs to be tested for its value, including:

Start position non-negative

The end position needs to be greater than the start position

The starting position needs to be less than the file length minus one (because the position index here is starting from 0)

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

  

Reference articles

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/

HTTP Status Codes 206 and 416

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.