码迷,mamicode.com
首页 > Web开发 > 详细

NSURLConnection的使用

时间:2016-03-16 22:28:27      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

一: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的使用

标签:

原文地址:http://www.cnblogs.com/dongfangchun/p/5285176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!