HTTP HEAD Generation method
Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0 timeoutInterval:kTimeout];
Request. HttpMethod = @ "HEAD";
[Nsurlconnection sendasynchronousrequest:request queue:self.myQueue completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
NSLog (@ "%@", response);
NSLog (@ "---------------");
NSLog (@ "%@", data);
}];
Run the test code to discover that the Head method simply returns the resource information without returning the data body
Application Scenarios:
(1) Get Resources mimetype (2) Gets the resource file size for endpoint continuation or multi-threaded downloadways to use code blocks to get the size of a resource
-(void) Filesizewithurl: (nsurl *) URL completion: (void (^) (long long contentlength)) completion
{
Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0 timeoutInterval:kTimeout];
Request. HttpMethod = @ "HEAD";
Nsurlresponse *response = nil;
[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];
Completion (RESPONSE.EXPECTEDCONTENTLENGTH);
}
determine the pseudo-code implementation of each download packet
-(void) Downloadfilewithurl: (nsurl *) URL
{
[Self Filesizewithurl:url completion:^ (long long ContentLength) {
NSLog (@ "Total file size:%lld", contentlength);
Download files by size
while (ContentLength > Kdownloadbytes) {
NSLog (@ "Each download length:%lld", (Long Long) kdownloadbytes);
ContentLength-= kdownloadbytes;
}
NSLog (@ "Last downloaded bytes:%lld", contentlength);
}];
}
Segmented Range Code Implementation
Long long frombytes = 0;
Long long tobytes = 0;
while (ContentLength > Kdownloadbytes) {
Tobytes = frombytes + kDownloadBytes-1;
NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", Frombytes, tobytes];
NSLog (@ "range%@", range);
Frombytes + = Kdownloadbytes;
ContentLength-= kdownloadbytes;
}
Frombytes = frombytes + contentLength-1;
NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", Frombytes, tobytes];
NSLog (@ "range%@", range);
Segment Download File
Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy: Nsurlrequestreloadignoringcachedata Timeoutinterval:ktimeout];
NSString *range = [NSString stringwithformat:@ "Bytes=%lld-%lld", from, end];
[Request Setvalue:range forhttpheaderfield:@ "range"];
Nsurlresponse *response = nil;
NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];
NSLog (@ "%@-%@-%ld", Range, response, (unsigned long) data.length);
Tips:
If get contains a range request header, the response is returned as a status code of 206 (partialcontent) instead of a (OK)
Writing data to a file
Open cache file
Nsfilehandle *FP = [Nsfilehandle FileHandleForWritingAtPath:self.cachePath];
If the file does not exist, write the data directly
if (!FP) {
[Data WriteToFile:self.cachePath Atomically:yes];
} else {
Move to end of file
[FP Seektoendoffile];
Append a data file to the end of a file
[FP Writedata:data];
Close file handle
[FP CloseFile];
}