(1) Post four ways to submit data
--content-type:application/x-www-form-urlencoded, the submitted data format is the Key1=value1&key2=value2 way.
Nsurl *url=nil; Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url cachepolicy:0 timeoutInterval:2.0f]; NSString *str=[nsstring stringwithformat:@ "username=%@&password=%@", self.name,self.pwd]; [Email protected] "POST"; Request. Httpbody=[str datausingencoding:nsutf8stringencoding]; [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue Alloc]init] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { }];
--multipart/form-data, the common way to upload binary files, but the upload file size cannot exceed 2M. The submitted data format is more complex, as follows (this data is also submitted to request.) Httpbody):
--Casual non-Chinese string content-disposition:form-data;name= ""; filename= "" Content-type:mime Type binary data to upload a file--casual non-Chinese string content-disposition:form-data;name= "submit" submit--random non-Chinese string--
--application/json, using the result of JSON serialization to assign to Httpbody, here can upload an object, of course, this object needs to go through the "object to Dictionary" processing, and the dictionary to the opposite of the object
ID obj=[person dictionarywithvaluesforkeys:@[@ "name", @ "pwd"]];request. Httpbody=[nsjsonserialization datawithjsonobject:obj options:0 Error:null];
--text/xml,web service technology, less use
(2) Put is another way to upload, but currently put and delete in the domestic web site development is rarely used, upload is generally used post.
(3) Nsurlsession and nsurlconnection are tied.
Urlsessionconfigration is set to Nsurlsession. Nsurlsession can control 3 kinds of tasks: Datatask,downloadtask and Uploadtask. When these tasks are created, they are "pending" and require a resume "on" to run.
Application Case:
Nsurl *tourl=nil; Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:tourl cachepolicy:0 timeoutInterval:2.0f]; [Email protected] "PUT"; Set user authorization nsstring *[email protected] "admin:123456"; NSData *authdata=[authstr datausingencoding:nsutf8stringencoding]; NSString *result=[authdata base64encodedstringwithoptions:0]; NSString *authstring=[nsstring stringwithformat:@ "Basic%@", result]; [Request setvalue:authstring forkey:@ "Authorization"]; Nsurl *fromurl=nil; Nsurlsession *session=[nsurlsession sharedsession]; Nsurlsessionuploadtask *task=[session uploadtaskwithrequest:request fromfile:fromurl completionHandler:^ (NSData * Data, Nsurlresponse *response, Nserror *error) { }]; [Task resume];
iOS Dev-99 post four ways to submit data and some knowledge of nsurlsession upload tasks