iOS implements a breakpoint continuation nsurlconnection

Source: Internet
Author: User

The key to nsurlconnection implementation of a breakpoint is to customize the range domain property of the header of the HTTP request.


Range header Field
The Range header field can request one or more child ranges of an entity. For example
Represents the first 500 bytes: bytes=0-499
Represents a second 500 byte: bytes=500-999
Represents the last 500 bytes: bytes=-500
Represents the range after 500 bytes: bytes=500-
First and last byte: Bytes=0-0,-1
Specify several ranges at the same time: bytes=500-600,601-999
However, the server can ignore this request header, and if the unconditional get contains a range request header, the response is returned as a status code of 206 (partialcontent) instead of a (OK).


Use Nsmutableurlrequest in iOS to define head fields

[CPP]View Plaincopyprint?
  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. }

iOS implements a breakpoint continuation nsurlconnection

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.