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.
- Get/down.zip http/1.1
- Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, application/vnd.ms-
- Excel, Application/msword, Application/vnd.ms-powerpoint, */*
- Accept-language:zh-cn
- Accept-encoding:gzip, deflate
- user-agent:mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
- 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:
- 200  
- content-length= 106786028  
- accept-ranges=bytes
- Date=mon, 30 apr 2001 56:11 gmt
- etag=w/
- content-type=application/ Octet-stream
- server=microsoft-iis/5.0  
- 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.
- Get/down.zip http/1.0
- User-agent:netfox
- range:bytes=2000070-
- 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:
- 206  
- content-length= 106786028  
- Content-range=bytes 2000070-106786027/
- date=mon, 30 apr 2001 12:55:20 gmt
- etag=w/ "02ca57e173c11:95b"
- content-type=application/octet-stream
- Server=microsoft-iis/5.0  
- 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:
- 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.
- Nsurl *url1=[nsurl urlwithstring:@"";
- nsmutableurlrequest* Request1=[nsmutableurlrequest REQUESTWITHURL:URL1];
- [request1 setvalue:@"bytes=20000-" forhttpheaderfield:@"Range"];
- [Request1 Setcachepolicy:nsurlrequestreloadignoringlocalcachedata];
- NSData *returndata1 = [nsurlconnection sendsynchronousrequest:request1 returningresponse:nil Error:nil];
- [Self writetofile:returndata1 filename:@"Somepath"];
- -(void) WriteToFile: (NSData *) data filename: (NSString *) filename
- {
- NSString *filepath=[nsstring stringwithformat:@"%@", FileName];
- if ([[Nsfilemanager defaultmanager] fileexistsatpath:filepath] = = NO) {
- NSLog (@"File not exist,create it ...");
- [[Nsfilemanager Defaultmanager] Createfileatpath:filepath contents:nil Attributes:nil];
- }Else {
- NSLog (@"file exist!!!");
- }
- FILE *file = fopen ([fileName utf8string], [@"ab+" utf8string]);
- if (file! = NULL) {
- fseek (file, 0, seek_end);
- }
- int readsize = [data length];
- Fwrite ((const void *) [data bytes], readsize, 1, file);
- fclose (file);
- }
- From:http://www.cnblogs.com/liyufeng2013/p/3826323.html
"Turn" Breakpoint Relay