Today is only second day, yesterday is home so long since the only let go of a good fun day, play very happy. After the happy and began to continue to study and review. First of all, may the new Year all the best, the most important health
First, let's talk about how to get information about downloading resources.
NSString *url_string = @ "Http://box.dwstatic.com/skin/Teemo/Teemo_Splash_0.jpg";
Nsmutableurlrequest *request =[nsmutableurlrequest requestwithurl:[nsurl urlwithstring:url_string] CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
Request. HttpMethod = @ "HEAD";
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
NSLog (@ "%@", response);
NSLog (@ "---------------");
NSLog (@ "%@", data);
}
This method only obtains information about the downloaded resource and does not return the data body.
Besides this there are other ways to get the size
Nsurl *url = [nsurl urlwithstring:url_string];//turn into URL
Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy TIMEOUTINTERVAL:30.F]; Create a variable network request
[Request sethttpmethod:@ "GET"];
[Nsurlconnection connectionwithrequest:request delegate:self];
Implementing a Proxy
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response
{
NSLog (@ "To download a file size of%@", response);
}
As to how to download this document, I believe there are many ways.
As for file upload.
You can create an upload model first.
- #import "UploadFile.h"
- @implementation UploadFile
- Stitching strings
- Static NSString *boundarystr = @ "--"; //delimited string
- static NSString *randomidstr; //This upload flag string
- static NSString *uploadid; //Upload (PHP) script, receive file fields
- -(Instancetype) init
- {
- Self = [super Init];
- if (self) {
- Randomidstr = @ "Itcast";
- Uploadid = @ "UploadFile";
- }
- return self ;
- }
- #pragma mark-Private method
- -(NSString *) Topstringwithmimetype: (nsstring *) MimeType uploadfile: (nsstring *) UploadFile
- {
- nsmutablestring *STRM = [nsmutablestring string];
- [StrM AppendFormat:@ "%@%@\n", Boundarystr, randomidstr];
- [StrM AppendFormat:@ "content-disposition:form-data; name=\"%@\ "; filename=\"%@\ "\ n", Uploadid, UploadFile];
- [StrM AppendFormat:@ "Content-type:%@\n\n", MimeType];
- NSLog (@ "%@", StrM);
- return [StrM copy];
- }
- -(NSString *) bottomstring
- {
- nsmutablestring *STRM = [nsmutablestring string];
- [StrM AppendFormat:@ "%@%@\n", Boundarystr, randomidstr];
- [StrM appendString:@ "content-disposition:form-data; name=\" submit\ "\ n"];
- [StrM appendString:@ "submit\n"];
- [StrM AppendFormat:@ "%@%@--\n", Boundarystr, randomidstr];
- NSLog (@ "%@", StrM);
- return [StrM copy];
- }
- #pragma mark-Upload file
- -(void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data
- {
- //1> Data Body
- nsstring *topstr = [self topstringwithmimetype:@ "Image/png" uploadfile:@ "Avatar 1.png"];
- nsstring *bottomstr = [self bottomstring];
- nsmutabledata *datam = [nsmutabledata data];
- [Datam appenddata:[topstr datausingencoding:nsutf8StringEncoding];
- [Datam Appenddata:data];
- [Datam appenddata:[bottomstr datausingencoding:nsutf8StringEncoding];
- //1. Request
- nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0 timeOutInterval:2. 0f];
- //Datam out of Scope will be released, so no copy
- Request. Httpbody = Datam;
- //2> Set the header property of the request
- Request. HttpMethod = @ "POST";
- //3> settings Content-length
- nsstring *strlength = [NSString stringwithformat:@ "%ld", (long) Datam. length];
- [Request Setvalue:strlength Forhttpheaderfield:@ "Content-length"];
- //4> settings Content-type
- nsstring *strcontenttype = [NSString stringwithformat:@ "multipart/form-data; boundary=%@", RANDOMIDSTR];
- [Request Setvalue:strcontenttype Forhttpheaderfield:@ "Content-type"];
- //3> connection server send request
- [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue alloc] init] completionhandler:^ (nsurlresponse *response, nsdata *data, nserror *connectionerror) {
- nsstring *result = [[NSString alloc] initwithdata:data encoding:nsutf8StringEncoding];
- NSLog (@ "%@", result);
- }];
- }
- @end
Then directly in the controller call-(void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data.
About the upload of code from http://blog.csdn.net/codywangziham01/article/details/38044637, this blog about the download and upload of the document is a lot of detailed explanation.
After the new year to continue learning, let us learn how to calculate the download file size, and how to post upload files