首页 技术 正文
技术 2022年11月9日
0 收藏 605 点赞 2,742 浏览 2756 个字

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]
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902