通过 NSURLConnection 发送 HTTP GET /HTTP POST /HTTP DELETE /HTTP PUT 请求

问题:

通过 HTTP 协议向服务器发送一个 GET 请求,并在这个请求中添加了一些参数. 
 
讨论:
GET 请求允许通过查询字符串当做参数,如下格式: 
http://example.com/?param1=value1&param2=value2...

你可以使用字符串格式来提供参数。 

  为了使用 NSURLConnection 模拟以 GET 请求方式发送一个查询字符串参数到网络服务器上,需要使用可修改的 URL 请求,并使用 NSMutableURLRequest 的 setHTTPMethod:将HTTP 的请求方法设置为 GET,以及将你的查询字符串参数作为 URL 的一部分,如下所示: 
  
- (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 was downloaded."); } else if (connectionError != nil){ NSLog(@"Error happened = %@", connectionError); } }]; }
  唯一一点值得我们需要注意的就是,发送的带参数的 GET 请求,第一个参数前面必须 要添加一个“?”,然后每个参数之间再用"&"分开,这样就表示传递了多个参数 

HTTP POST:

问题:

通过 HTTP POST 方法请求一个 web 服务,有可能会发送一些参数(作为HTTPbody 或者查询参数)到 web 服务上。 
- (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 was downloaded.");
        }
        else if (connectionError != nil){
            NSLog(@"Error happened = %@", connectionError);
        }
    }];
}
在 HTTP body 中发送的第一个参数不需要?前缀,这跟查询字符串中的第一个参数不同。 

HTTP DELETE:

  使用 HTTP DELETE 方法调用一个 web 服务,以删除一个资源。可能会传递一 些参数到 web 服务中,这些参数可能在 HTTP body 中或者查询字符串中。 
  就像发送 GET 和 POST 方法一样,我们也可以使用 NSURLConnection 来发送请求。我 们必须明确的将 URL 请求方法设置成 DELETE. 
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"DELETE"];
NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2"; 
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
HTTP PUT 请求:
向服务器发送 HTTP PUT 请求,以向 web 服务中放置一些资源,有可能在请求中 带一些参数:HTTP body 或查询参数。 
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"PUT"];
NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2"; [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
原文地址:https://www.cnblogs.com/safiri/p/4089559.html