IOS学习之路十八(通过 NSURLConnection 发送 HTTP 各种请求)

你想通过 Http 协议向服务器发送一个 Get 的包装请求,并在这个请求中添加了一些请

求参数.

向远程服务器发送一个 GET 请求,然后解析返回的数据。通常一个 GET 请求是添加了

一些参数的,这些参数一般是添加在 URL 请求中。
我准备了一个
GET 形式的 webservice 接口,你可以通过 http://pixolity.com/get.php 来进

行请求。

/* URL = http://pixolity.com/get.php?param1=First¶m2=Second */ NSString *urlAsString = @"http://pixolity.com/get.php";
urlAsString = [urlAsString stringByAppendingString:@"?param1=First"]; urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,

NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html); }
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded."); }
else if (error != nil){
NSLog(@"Error happened = %@", error);
} }];

post请求:

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:30.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 *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html); }
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded."); }
else if (error != nil){
NSLog(@"Error happened = %@", error);
} }];

delete请求:

    NSString *urlAsString = @"http://pixolity.com/delete.php";
    urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
    urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
    NSURL *url = [NSURL URLWithString:urlAsString];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    [urlRequest setTimeoutInterval:30.0f];
    [urlRequest setHTTPMethod:@"DELETE"];
    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 *error) {
         if ([data length] >0 && error == nil){
             NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
             NSLog(@"HTML = %@", html); }
         else if ([data length] == 0 && error == nil){
             NSLog(@"Nothing was downloaded."); }
         else if (error != nil){
             NSLog(@"Error happened = %@", error);
         } }];

put请求:

 NSString *urlAsString=@"http://pixolity.com/put.php";
    urlAsString=[urlAsString stringByAppendingString:@"?param1=First"];
    urlAsString=[urlAsString stringByAppendingString:@"¶m2=Second"];
    NSURL *url=[NSURL URLWithString:urlAsString];
    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    [urlRequest setTimeoutInterval:30.0f];
    [urlRequest setHTTPMethod:@"PUT"];
    NSString *body=@"bodyParaml=BodyValuel&bodyParam2=BodyValue2";
    [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if ([data length]>0&&error==nil) {
            NSString *html=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"HTML=%@",html);
        }else if([data length]==0&&error==nil){
            NSLog(@"Nothing was downLoaded.");
        }else if(error!=nil){
            NSLog(@"Error Happened=%@",error);
        }
    }];

转载请注明:

本文转自:点击打开链接http://blog.csdn.net/wildcatlele

新浪微博:http://weibo.com/u/3202802157







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值