OC - 12.NSURLRequest与NSURLConnection

##NSURLRequest


  • NSURLRequest封装了一次网络请求所需要的数据,主要封装了以下信息:

    • 请求路径(URL)
    • 请求方法(GET或POST)
    • 请求头
    • 请求体
    • 超时参数
  • NSURLRequest与其子类NSMutableURLRequest

    • NSURLRequest的所有的请求信息拼接在请求路径(URL)的后面
    • NSMutableURLRequest的请求路径与其他的请求信息分开,其他请求信息通过对应的Key对请求对象进行设置
    • NSURLRequest通常用于GET请求
    • NSMutableURLRequest通常用于POST请求
  • NSURLRequest封装一次网络请求的的步骤

    //1.创建请求路径
    NSString *strURL = [NSString stringWithFormat:@"(此处为URL)/login?username=%@&pwd=%@", @"用户名", @"密码"]; NSURL *url = [NSURL URLWithString:]; //2.根据请求路径封装请求 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  • NSMutableURLRequest封装一次网络请求的的步骤

    //1.创建请求路径
    NSURL *url = [NSURL URLWithString:@"(此处为URL)/login"];
    //2.创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //3.设置请求方法
    request.HTTPMethod = @"POST"; //4.设置请求参数 request.HTTPBody = [@"username="用户名"&pwd="密码" dataUsingEncoding:NSUTF8StringEncoding]; //5.设置超时 request.timeoutInterval = 5; 

##NSURLConnection


  • NSURLConnection发送请求的步骤

    • 创建请求路径(NSURL)
    • 将请求路径封装成请求对象(NSURLRequest),设置其他请求参数
    • 使用NSURLConnection发送同步/异步请求
  • NSURLConnection的代理

    • NSURLConnectionDelegate

      - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      /**
      *遇到错误的时候调用,请求终止 */ 
    • NSURLConnectionDataDelegate

      - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      /**
      *接收到服务器响应的时候调用 *response的中包含了服务器的响应信息,比较有价值是此次请求的数据的总长度 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data /** *接收到服务器的数据的时候调用,若数据较多会多次调用 *通常在该方法中对服务器返回的数据进行存储 *也可以在该方法中计算下载进度 */ - (void)connectionDidFinishLoading:(NSURLConnection *)connection /** *数据加载完毕的时候调用 */ 
    • NSURLConnectionDownloadDelegate

      - (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes /** *每次向沙盒中写文件都会调用该方法 */ - (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes /** *该方法是支持断点下载的核心 */ - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *) destinationURL /** *由于:下载的文件保存在tmp文件夹中,该文件夹中的数据会被系统定时删除 *所以该方法必须实现,用于将改变数据的存储位置 */ 
  • NSURLConnection的请求方式

    • 同步请求(线程会被阻塞)

      NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
      /**
      *data:服务器返回的数据,即请求的数据
      *request:请求请求对象 *response:服务器的响应数据 *error:错误信息 */ 
    • 异步请求

      //方法一(block)
      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
          /** *请求完成回调的block,参数的含义与铜鼓请求相同 */ }]; //方法二(代理) [NSURLConnection connectionWithRequest:request delegate:self] /** *自动发送请求 */ NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; /** *需要手动发送请求 */ 

##URL中的中文处理


  • URL中的中文通要进行处理,通常使用UTF-8编码

    //进行如下转码
    [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
    

     

PS.在Xcode7中,NSURLConnection已经废除了,不能再使用,现在使用的NSURLSession进行代替
 

转载于:https://www.cnblogs.com/funny11/p/4933991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值