11.5~11.8 GET POST DELETE PUT HTTP Request

11.5. Sending HTTP GET Requests with NSURLConnection

发送get请求并传递一些参数

-(void)testGetHttp

{

//    NSString *urlAsString = @"http://www.baidu.com/s";//百度的数据在解析成字符串时失败

    NSString *urlAsString =@"http://www.apple.com";

    urlAsString = [urlAsStringstringByAppendingString:@"?wd=abc"];

    urlAsString = [urlAsStringstringByAppendingString:@"&ie=utf-8"];

   NSURL *url = [NSURLURLWithString:urlAsString];

    //这里初始化时设定超时

    //    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

   NSURLRequest *urlRequest = [NSURLRequestrequestWithURL:url

                                                cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData

                                           timeoutInterval:30.0f];

   NSURLResponse *response = nil;

   NSError *error = nil;

    NSLog(@"will send ");

   NSData * data = [NSURLConnectionsendSynchronousRequest:urlRequest returningResponse:&response error:&error];

    

   if ([data length] >0 && error ==nil){

        NSLog(@"length=%d,html=%@",data.length,[[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding]);

       NSLog(@"%@",response.textEncodingName);


        /* Append the filename to the documents directory */

       NSString * sfilePath = NSTemporaryDirectory();

        sfilePath = [sfilePathstringByAppendingString:@"apple.html"];

//        NSLog(@"sfilePath = %@",sfilePath);

       NSURL * filePath = [NSURLfileURLWithPath:sfilePath];

//        NSLog(@"filePath = %@",filePath);

        [datawriteToURL:filePath atomically:YES];

//        NSLog(@"Successfully saved the file to %@", filePath);

    }elseif ([data length] ==0 && error == nil){

        NSLog(@"Nothing was downloaded.");

    }

   else if (error !=nil){

        NSLog(@"Error happened = %@", error);

    }

   NSLog(@"done");

    

}


2014-06-24 17:14:37.231 cookbook7[1145:a0b] will send 

2014-06-24 17:14:38.510 cookbook7[1145:a0b] length=12559,html=<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">

<head>

……

</head>

<body class="home" id="home">

……

</body>

</html>

2014-06-24 17:14:38.515 cookbook7[1145:a0b] utf-8

2014-06-24 17:14:38.516 cookbook7[1145:a0b] done


11.6. Sending HTTP POST Requests with NSURLConnection

post方法请求

-(void)testPostHttp

{

    NSString *urlAsString = @"http://www.baidu.com/s";

    urlAsString = [urlAsStringstringByAppendingString:@"?param1=First"];

    urlAsString = [urlAsStringstringByAppendingString:@"&param2=Second"];

   NSURL *url = [NSURLURLWithString:urlAsString];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequestrequestWithURL:url];

    [urlRequestsetTimeoutInterval:30.0f];

    [urlRequestsetHTTPMethod:@"POST"];

    NSString *body =@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";

    [urlRequest setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];

    NSOperationQueue *queue = [[NSOperationQueuealloc] init];

    [NSURLConnection

     sendAsynchronousRequest:urlRequest

    queue:queue

    completionHandler:^(NSURLResponse *response,

                        NSData *data,

                        NSError *error) {

        if ([data length] >0 && error ==nil){ NSString *html =

             [[NSStringalloc] initWithData:data

                                  encoding:NSUTF8StringEncoding];

            NSLog(@"HTML = %@", html);

         }

        else if ([datalength] == 0 && error ==nil){

             NSLog(@"Nothing was downloaded.");

         }

        else if (error !=nil){

            NSLog(@"Error happened = %@", error);

         }

     }];

}


打印:

2014-06-24 17:45:53.710 cookbook7[1218:4307] HTML = (null)

百度不接收这样的post请求的,结果就这样咯



11.7. Sending HTTP DELETE Requests with NSURLConnection

11.511.6中得方法改下即可

 [urlRequestsetHTTPMethod:@"DELETE"];



11.8. Sending HTTP PUT Requests with NSURLConnection

上传资源到服务器

我们就试着给百度发点东西吧,看他会怎样

-(void)testPutHttp

{

    NSString *urlAsString =@"http://www.baidu.com/s";

    urlAsString = [urlAsStringstringByAppendingString:@"?param1=First"];

    urlAsString = [urlAsStringstringByAppendingString:@"&param2=Second"];

   NSURL *url = [NSURLURLWithString:urlAsString];

    NSMutableURLRequest *urlRequest =

    [NSMutableURLRequestrequestWithURL:url];

    [urlRequestsetTimeoutInterval:30.0f];

    [urlRequestsetHTTPMethod:@"PUT"];

    NSString *body =@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";

    [urlRequest setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];

    NSOperationQueue *queue = [[NSOperationQueuealloc] init];

    [NSURLConnection

     sendAsynchronousRequest:urlRequest

    queue:queue

    completionHandler:^(NSURLResponse *response,

                        NSData *data,

                        NSError *error) {

        if ([data length] >0 && error ==nil){ NSString *html =

             [[NSStringalloc] initWithData:data

                                  encoding:NSUTF8StringEncoding];

            NSLog(@"HTML = %@", html);

         }

        else if ([datalength] == 0 && error ==nil){

             NSLog(@"Nothing was downloaded.");

         }

        else if (error !=nil){

            NSLog(@"Error happened = %@", error);

         }

     }];

}


打印:

2014-06-24 17:50:50.609 cookbook7[1236:1403] Nothing was downloaded.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值