• iOS中HTTP请求小结


    iOS SDK为HTTP请求提供了同步异步请求两种不同的API,而且可以使用Get或Post等请求方法。

    1、发送 “同步、Get” 请求

    - (void)startRequest

    {

      NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php?email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];   【1】

      NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];                           【2】

      NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];                            【3】

      NSData *data = [NSURLConnection sendSynchronousRequest:request  returningReponse:nil  error:nil];   【4】

      NSLog(@"请求完成....");

      NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

      ........

    }

    【1】:指定请求的URL。请求的参数全部暴露在URL后面(说明URL不包括请求参数),这是Get请求方法的特定

    【2】:创建NSURL对象。[strUrl URLEncodedString]将strUrl字符串转换为URL字符串,在Internet传输的时候,URL中不能有中文等特殊字符,使用URLEncodedString就是把这些特殊字符转化为有百分号的URL编码。

    【3】:创建NSURLRequest对象,可以使用更为复杂的形式initWithURL:cachePolicy:timeoutInterval设置缓存策略和超时时长。

    【4】:使用NSURLConnection的sendSynchronousRequest:  returningReponse: error:同步方法进行请求,返回NSData类型的数据。所谓同步方法就是请求过程中线程阻塞到这里,直到服务器应答返回回来为止。

    2、发送“异步、Get”请求

    异步请求会使用NSURLConnection委托协议NSURLConnectionDelegate。

    NSURLConnectionDelegate协议的方法有:

    connection:didReciveData:请求成功,开始接收数据,如果数据量很多,可能会被多次调用

    connection:didFailWithError:加载数据出现异常

    connectionDidFinishLoading:成功完成加载数据,在connection:didReciveData:方法之后执行。

    - (void)startRequest

    {

      NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php?email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"];   

      NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];                           

      NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];                            

      NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request

                                                                                                            delegate:self ];

      if (connection)

      {

        _datas = [[NSMutableData  alloc] init];

      }

    }

    #pragma mark - NSURLConnection代理方法

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    {

      [_datas appendData:data];
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection

    {

      NSLog(@"请求完成....");

      NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

      。。。

    }

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    {

    }

    从上面两个Get请求方式可以看出:Get请求方式与同步请求还是异步请求无关。

    3、发送“异步、Post”请求

    使用Post方法请求的关键是:使用NSMutableURLRequest类替代NSURLRequest。

    - (void)startRequest

    {

      NSString *strUrl = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php"];   

      NSURL *url = [NSURL URLWithString:[strUrl URLEncodedString]];  

      // 准备请求体

      NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@",@"ios_yaoxinchao@163.com",@"JSON",@"query"] ;

      NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];      【1】                 

      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  

      [request setHTTPMethod:@"POST"];

      [request setHTTPBody:postData];      //  HTTPBody部分是请求参数。                  

      NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request

                                                                                                            delegate:self ];

      if (connection)

      {

        _datas = [[NSMutableData  alloc] init];

      }

    【1】:将参数字符串转换成NSData类型,编码一定要采用UTF-8.

    注:设置request请求头

    [ request  setValue :@“application/soap+xml;charset=utf-8”  forHTTPHeaderField:@"Content-Type" ] ;

  • 相关阅读:
    转Asktom:Pipelined Functions
    转:PVE法师附魔
    附魔300375图纸掉落大全
    转:Oracle直方图详解
    转:JSON的序列化及GET异步调用.
    转:ORACLE 中dbms_stats的使用
    jQuery中$.each的用法
    魔兽世界天赋详解之 法师篇 一冰法
    Miley's Oracle讲堂第三课:如何在Oracle中使用对象表存储数据.
    台服体验之急速升级
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3717637.html
Copyright © 2020-2023  润新知