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¶m2=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:@"¶m2=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:@"¶m2=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