HTTP request on iOS: Post method

Source: Internet
Author: User

1NSString * URLString =@"www.baidu.com";2Nsurl * URL =[Nsurl urlwithstring:[urlstring stringbyaddingpercentescapesusinge ncoding:nsutf8stringencoding];   4NSString * poststring =@"theregioncode= Hunan";5NSData * PostData = [poststring datausingencoding:nsutf8stringencoding];//Convert request parameter string to NSData type    7Nsmutableurlrequest * Request =[[Nsmutableurlrequest alloc]init];8[Request Sethttpmethod:@"Post"];//Specify the request method9[Request Seturl:url];//set the requested addressTen[Request Sethttpbody:postdata];//set parameters for the request One       ANsurlresponse *response; -Nserror *error; -NSData * Backdata = [nsurlconnection sendsynchronousrequest:request returningres ponse:&response error:&ERROR];       -     if(Error) { -NSLog (@"Error:%@", [Error localizeddescription]); -}Else{ +NSLog (@"Response:%@", response); -NSLog (@"backdata:%@", [[NSString alloc]initwithdata:backdata Encoding:nsutf 8StringEncoding]); +}

Post mode when the parameters are placed inside the httpbody, and the need to transcode the string into a response to the NSData type, in the interface document is generally indicated in the Transcoding method, the need to transcode in the specified way, here the UTF8, there are gb2312. After the request has been built, use nsurlconnection as the Get method for requesting data.

HTTP request on iOS: Post method

Related Article

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.