NSURLConnection的使用

Posted 东方春

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSURLConnection的使用相关的知识,希望对你有一定的参考价值。

一:NSURLConnection(ios9.0已经弃用)是早期apple提供的http访问方式。以下列出了常用的几个场景:GET请求,POST请求,Response中带有json数据

      对于NSURLConnection有以下注意事项:(1)sendAsynchronourequest: queue: completionHandler:函数中的queue参数表示的是“handler 这个block运行在queue中,如果queue为mainThread,那么hanlder就运行在主线程;所以在处理UI的时候需要注意这个参数”

(1)Get请求(返回文本)

  //Request
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest new]; 
    [urlRequest setURL:[NSURL URLWithString:@"http://XXX.sinaapp.com/test/test.php?namr&id=43"]];
    [urlRequest setTimeoutInterval:10.0f];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:urlRequest
                                       queue:queue
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
    
    //根据回复Headers,确认是是否为NSHTTPURLResponse的对象    if([response isKindOfClass:[NSHTTPURLResponse class]]){    NSHTTPURLResponse *resHttp = (NSHTTPURLResponse *)response;    NSLog(@"status = %ld",resHttp.statusCode);//200 304 401......    NSDictionary *dicHeader = resHttp.allHeaderFields;   NSLog(@"headers = %@",dicHeader);    }    else{    NSLog(@"not http");   }
  
if(data){    NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"%@",html);    } }];

 

 
(2)POST请求(返回文本)
    //Request

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest new];

    [urlRequest setURL:[NSURL URLWithString:@"http://XXX.sinaapp.com/test/test.php"]];
    [urlRequest setTimeoutInterval:10.0f];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    NSString *strBody = @"p1=abc&p2=12";
    [urlRequest setHTTPBody:[strBody dataUsingEncoding:NSUTF8StringEncoding]];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:
^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //确认是http if([response isKindOfClass:[NSHTTPURLResponse class]]){ NSHTTPURLResponse *resHttp = (NSHTTPURLResponse *)response; NSLog(@"status = %ld",resHttp.statusCode);//200 304 401...... NSDictionary *dicHeader = resHttp.allHeaderFields; NSLog(@"headers = %@",dicHeader); } else{ NSLog(@"not http"); } if(data){ NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",html); } }];

 

    

(3)Response中有Json数据
    //Request

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest new];

    [urlRequest setURL:[NSURL URLWithString:@"http://XXX.sinaapp.com/test/test.php"]];
    [urlRequest setTimeoutInterval:10.0f];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    NSString *strBody = @"p1=abc&p2=12";
    [urlRequest setHTTPBody:[strBody dataUsingEncoding:NSUTF8StringEncoding]];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    [NSURLConnection
     sendAsynchronousRequest:urlRequest
     queue:queue
     completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
         NSError *err2 = nil;
         id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err2];

         if([jsonObject isKindOfClass:[NSDictionary class]]){
             NSLog(@"NSDictionary");
             NSDictionary *dic = jsonObject;
             NSLog(@"dic = %@",dic);
         }
         else if([jsonObject isKindOfClass:[NSArray class]]){
             NSLog(@"NSDictionary");
             NSDictionary *arr = jsonObject;
             NSLog(@"arr = %@",arr);
         }
     }];

 

(4)Request中带有Json格式数据

 

以上是关于NSURLConnection的使用的主要内容,如果未能解决你的问题,请参考以下文章

停止/中止 NSURLConnection

使用 NSURLConnection 从 Dropbox 中选择选项

NSURLConnection使用

使用 NSURLConnection 和 NSURLProtectionSpace 确定信任

使用 NSURLConnection 网络服务

如何在情节提要中使用 NSURLConnection 下载文件