1 , HTTP HEAD 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) Access to resources mimetype
(2) Get the resource file size for endpoint continuation or multi-threaded download
2 , using block code to get the network resource size method
-(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);
}
3 , 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);
}];
}
4 , HTTP Range The example
n Specify the size of each packet to be downloaded from the network by setting range
N Range Example
N bytes=0-499 from 0 to 499 of the first 500 bytes
N bytes=500-999 the second 500 bytes from 500 to 999
N bytes=500-all bytes from 500 bytes
n bytes=-500 last 500 bytes
n bytes=500-599,800-899 specify several ranges at the same time
N Range Summary
N-Used to separate
The number in front of P indicates the number of starting bytes
The array after p represents the number of cutoff bytes and does not represent to the end
n is used for grouping, you can specify more than one range at a time, but rarely
5 , 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);
6 , segment Download File
/**nsurlrequestuseprotocolcachepolicy = 0//default cache policy, memory cache
Nsurlrequestreloadignoringlocalcachedata = 1//ignores local memory cache */
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)
7 , writing data to a file
-(void) AppendData: (NSData *) data
{
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];
}
}
8 , Check File size
Determine if a file exists
if ([[[Nsfilemanager Defaultmanager] FileExistsAtPath:self.cachePath]) {
Nsdictionary *dict = [[Nsfilemanager Defaultmanager] AttributesOfItemAtPath:self.cachePath error:null];
return [dict[nsfilesize] longlongvalue];
} else {
return 0;
}
Tip: Because the data is appended, in order to avoid repeatedly downloading files from the network, before downloading
- Determine if a file already exists in the cache path
- Check file size if present
- If the file size matches the size of the network resource, it is no longer downloaded
iOS development files (segmented) download