Sending an HTTP get/http POST request via Nsurlconnection

Source: Internet
Author: User

Problem:

A GET request is sent to the server via the HTTP protocol and some parameters are added to the request. Discussion: A GET request allows a query string to be used as a parameter, in the following format: Http://example.com/?param1=value1&param2=value2 ...

You can use string formatting to provide parameters.

In order to use nsurlconnection impersonation to send a query string parameter to a network server using a GET request, you need to use a modifiable URL request and use the Nsmutableurlrequest Sethttpmethod: the HTTP The request method is set to GET, and your query string parameter as part of the URL, as follows:
- (void) sendhttpget{NSString*urlasstring =@"http://pixolity.com/get.php"; Urlasstring= [Urlasstring stringbyappendingstring:@"? Param1=first"]; Urlasstring= [Urlasstring stringbyappendingstring:@"&param2=second"];
Nsmutableurlrequest*request =[Nsmutableurlrequest Requestwithurl:[nsurl urlwithstring:urlasstring]; [Request Settimeoutinterval:10.0f]; [Request Sethttpmethod: @ "GET" ]; Nsoperationqueue*queue =[[Nsoperationqueue alloc]init]; [Nsurlconnection sendasynchronousrequest:request queue:queue Completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) { if([Data length] >0&& Connectionerror = =Nil) {NSString*html =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@"HTML =%@", HTML); } Else if([data length] = =0&& Connectionerror = =Nil) {NSLog (@"Nothing is downloaded."); } Else if(Connectionerror! =Nil) {NSLog (@"Error happened =%@", Connectionerror); } }];}
The only thing that's worth noting is that a GET request with parameters is sent, and the first parameter must be preceded by a "?", and then separated by "&" between each parameter, which means that more than one parameter is passed

HTTP POST:

Problem:

Requesting a Web service via the HTTP POST method, it is possible to send some parameters (as httpbody or query parameters) to the Web service.  
- (void) sendhttppost{NSString*urlasstring =@"http://pixolity.com/post.php"; Urlasstring= [Urlasstring stringbyappendingstring:@"? Param1=first"]; Urlasstring= [Urlasstring stringbyappendingstring:@"&param2=second"]; Nsurl*url =[Nsurl urlwithstring:urlasstring]; Nsmutableurlrequest*urlrequest =[Nsmutableurlrequest Requestwithurl:url]; [URLRequest settimeoutinterval:10.0f]; [urlrequest sethttpmethod:  @ "POST"]; NSString *body = @ "bodyparam1=bodyvalue1&bodyparam2=bodyvalue2" ;        [URLRequest sethttpbody:[body datausingencoding:nsutf8stringencoding]; Nsoperationqueue*queue =[[Nsoperationqueue alloc]init]; [Nsurlconnection sendasynchronousrequest:urlrequest queue:queue Completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {        if([Data length] >0&& Connectionerror = =Nil) {NSString*html = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@"HTML =%@", HTML); }        Else if([data length] = =0&& Connectionerror = =Nil) {NSLog (@"Nothing is downloaded."); }        Else if(Connectionerror! =Nil) {NSLog (@"Error happened =%@", Connectionerror); }    }];}
The first parameter sent in the HTTP body does not need a prefix, which is different from the first parameter in the query string.

HTTP DELETE:

Use the HTTP Delete method to invoke a Web service to delete a resource.   Some parameters may be passed to the Web service, which may be in the HTTP body or in the query string. Just like sending a GET and POST method, we can also use Nsurlconnection to send requests. We must explicitly set the URL request method to DELETE.

[URLRequest settimeoutinterval:30.0f]; [URLRequest Sethttpmethod: @" DELETE "  @ "bodyparam1=bodyvalue1&bodyparam2=bodyvalue2"
[URLRequest sethttpbody:[body datausingencoding:nsutf8stringencoding];

HTTP PUT request: sends an HTTP PUT request to the server to place some resources into the Web service, possibly with some parameters in the request: HTTP body or query parameters.
[URLRequest settimeoutinterval:30.0f]; [URLRequest Sethttpmethod: @" PUT "  @ "bodyparam1=bodyvalue1&bodyparam2=bodyvalue2"; [URLRequest sethttpbody:[body datausingencoding:nsutf8stringencoding];

Sending an HTTP get/http POST request via Nsurlconnection

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.