zoukankan      html  css  js  c++  java
  • 多线程与网络之NSURLConnection发送请求

    HTTP

    • 1.简介
      • 1.URL:统一资源分配符
      • 2.协议名://主机地址/路径
      • 3.HTTP: 超文本传输协议

    NSURLConnection

    • 1.基本简介:

      • 1.1 NSURLRequest: 一个这对象代表一个请求,里面有:
        • 一个NSURL对象
        • 请求方法,请求头,请求体
        • 请求超时...
      • 1.2 NSMutableURLRequest: NSURLRequest的子类
    • 2.使用步骤:

      • 2.1 创建一个NSURL对象,设置请求路径(协议名://主机地址/路径 + ? + 参数1 & 参数2)
      • 2.2 传入NSURL创建一个NSURLRequest对象,设置请求头和请求体(默认就有)
      • 2.3 使用NSURLConnection发送请求(得到服务器返回的数据)
      • 2.5 解析服务器响应的数据(将NSData转成NSString)
    • 3.发送GET请求

      // 第一种:同步
      // 1.创建一个NSURL对象,设置请求路径
      NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=xiao&pwd=xiao&type=JSON"];
      
      // 2.传入NSURL创建一个NSURLRequest对象,设置请求头和请求体(默认存在)
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      
      // 3.使用NSURLConnection发送请求,得到服务器返回的NSData数据
      NSURLResponse *response = nil;  // 真实类型是NSHTTPURLResponse
      NSError *error = nil;
      /*
       第一个参数:请求对象
       第二个参数:响应头信息,传地址
       第三个参数:错误信息, 传地址
       */
      [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
      
      // 第二种:异步
      // 1.创建一个NSURL对象.设置请求路径
      NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
      // 2.创建一个NSURLRequest对象,设置请求头和请求体(默认存在)
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      // 3.使用NSURLConnection发送网络请求
      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] 
          completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
      
          NSLog(@"%@", [NSThread currentThread]);
          // 4.解析服务器响应的数据
          NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
          NSLog(@"%@", str);
      }];
      
      // 第三种:(异步代理)注意data一定要拼接
      - (void)sendAsyncDelegate
      {
      // 1.创建一个NSURL对象,设置请求路径
      NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
      // 2.创建一个NSURLRequest对象,设置请求头和请求体(默认存在)
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      
      // 3.使用NSURLConnection来发送请求(返回服务器响应的数据)
      //    [NSURLConnection connectionWithRequest:request delegate:self]; // 第一种
      //    [[NSURLConnection alloc] initWithRequest:request delegate:self]; // 第二种
      
      NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; // 第三种
      // 如果 startImmediately 设置成NO时,必须调用start方法调用
      [connect start];
      }
      // 实现代理方法
      // 1.当接受到服务器响应的时候调用
      - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      {
      self.dataM = [NSMutableData data];
      }
      // 2.当接收到服务器返回的二进制数据的时候调用, 会调用多次
      - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
      {
      [self.dataM appendData:data];
      }
      // 3.当请求失败或者错误的时候调用(请求超时)
      - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {}
      // 4.当请求结束的事后调用
      - (void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
      // 解析数据
      NSString *str = [[NSString alloc] initWithData:self.dataM encoding:NSUTF8StringEncoding];
      NSLog(@"%@", str);
      }
    • 4.发送POST请求

      // 1.创建NSURL
      NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
      
      // 2.创建NSMutableURLRequest
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
      // 2.1 设置NSMutableURLRequest的属性
      request.HTTPMethod = @"POST";
      request.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
      
      request.timeoutInterval = 10.0; //过期时间
      
      [request setValue:@"ios10" forHTTPHeaderField:@"User-Agent"];
      
      // 3.发送请求
      [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
      
          // 解析数据
          NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
      }];
  • 相关阅读:
    wxpython笔记:应用骨架
    go 优雅的检查channel关闭
    Golang并发模型:流水线模型
    go http数据转发
    go 互斥锁与读写锁
    go 工作池配合消息队列
    实现Tcp服务器需要考虑哪些方面
    go Goroutine泄露
    关于个人博客转移的那些事
    Java并发编程:Thread类的使用介绍
  • 原文地址:https://www.cnblogs.com/LongLJ/p/5084337.html
Copyright © 2011-2022 走看看