"Turn" Breakpoint Relay

Source: Internet
Author: User
Tags one more line

How the breakpoint continues to pass

In fact, the principle of the continuation of the breakpoint is very simple, is the Http request and the general download is different.
For example, when a browser requests a text on a server, the following requests are made:
Assume that the server domain name is wwww.sjtu.edu.cn and the file name is Down.zip.

    1. Get/down.zip http/1.1
    2. Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, application/vnd.ms-
    3. Excel, Application/msword, Application/vnd.ms-powerpoint, */*
    4. Accept-language:zh-cn
    5. Accept-encoding:gzip, deflate
    6. user-agent:mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
    7. Connection:keep-alive

After the server receives the request, it looks for the requested file, extracts the file information, and returns it to the browser, returning the following information:

    1. 200  
    2. content-length= 106786028  
    3. accept-ranges=bytes   
    4. Date=mon, 30 apr 2001 56:11 gmt   
    5. etag=w/
    6. content-type=application/ Octet-stream   
    7. server=microsoft-iis/5.0  
    8. Last-modified=mon, 30 apr 2001  12:56:11 GMT   

The so-called breakpoint continuation, that is, the file has been downloaded from the place to continue to download. So add a piece of information when the client browser passes to the WEB server-where to start.
The following is a "browser" made by yourself to pass the request information to the WEB server, starting from 2000070 bytes.

    1. Get/down.zip http/1.0
    2. User-agent:netfox
    3. range:bytes=2000070-
    4. Accept:text/html, Image/gif, Image/jpeg, *; Q=. 2, */*; q=. 2

Take a closer look and you'll find one more line range:bytes=2000070-
The meaning of this line is to tell the server Down.zip this file is transmitted from 2000070 bytes, the previous byte is not passed.
After the server receives this request, the following information is returned:

    1. 206  
    2. content-length= 106786028  
    3. Content-range=bytes 2000070-106786027/
    4. date=mon, 30 apr 2001 12:55:20 gmt   
    5. etag=w/ "02ca57e173c11:95b"    
    6. content-type=application/octet-stream   
    7. Server=microsoft-iis/5.0  
    8. last-modified=mon, 30 apr < Span class= "number" >2001 12:55:20 gmt    

Compare the information returned by the previous server and you will find an additional line:

    1. Content-range=bytes 2000070-106786027/106786028

The returned code is also changed to 206, not 200 anymore.

Knowing the above principles, you can proceed to the programming of the continuation of the breakpoint.

    1. Nsurl *url1=[nsurl urlwithstring:@"";
    2. nsmutableurlrequest* Request1=[nsmutableurlrequest REQUESTWITHURL:URL1];
    3. [request1 setvalue:@"bytes=20000-" forhttpheaderfield:@"Range"];  
    4. [Request1 Setcachepolicy:nsurlrequestreloadignoringlocalcachedata];
    5. NSData *returndata1 = [nsurlconnection sendsynchronousrequest:request1 returningresponse:nil Error:nil];
    6. [Self writetofile:returndata1 filename:@"Somepath"];
    7. -(void) WriteToFile: (NSData *) data filename: (NSString *) filename
    8. {
    9. NSString *filepath=[nsstring stringwithformat:@"%@", FileName];
    10. if ([[Nsfilemanager defaultmanager] fileexistsatpath:filepath] = = NO) {
    11. NSLog (@"File not exist,create it ...");
    12. [[Nsfilemanager Defaultmanager] Createfileatpath:filepath contents:nil Attributes:nil];
    13. }Else {
    14. NSLog (@"file exist!!!");
    15. }
    16. FILE *file = fopen ([fileName utf8string], [@"ab+" utf8string]);
    17. if (file! = NULL) {
    18. fseek (file, 0, seek_end);
    19. }
    20. int readsize = [data length];
    21. Fwrite ((const void *) [data bytes], readsize, 1, file);
    22. fclose (file);
    23. }
      1. From:http://www.cnblogs.com/liyufeng2013/p/3826323.html

"Turn" Breakpoint Relay

Related Article

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.