"iOS Dev-99" post four ways to submit data and some knowledge of nsurlsession upload tasks

Source: Internet
Author: User

(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

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.