1.为什么要使用网络?

2015-10-05  本文已影响232人  SoftKnife
People Lack Willpower,Rather Than Strength!

客户端和服务器之间如何通信?


HTTP


NSURLConnection

登录请求

    /*=======================get同步方法发送request================================*/
- (void)sendSync
{
    // NSURLConnection-->http采用get请求|sendSync...方案发送请求

    // 1.创建URL
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];

    // 2.创建request
    // 默认情况下NSURLRequest会自动给我们设置好请求头
    // request默认情况下就是GET请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 3.发送request
    /*
     第一个参数:需要请求的对象;
     第二个参数:服务返回给我们的响应头信息;
     第三个参数:错误信息;
     返回值:服务器返回给我们的响应体
     */
    //    NSURLResponse *response = nil;
    // response的真实类型是:
    NSHTTPURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    // 打印响应头/体信息
    NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSLog(@"%@",response.allHeaderFields);

    NSLog(@"touches...即将结束-------");

    /*
     2015-09-07 09:19:32.287 01-NSURLConnection基本使用[1116:143990] {"success":"登录成功"}
     2015-09-07 09:19:32.287 01-NSURLConnection基本使用[1116:143990] {
     "Content-Type" = "application/json;charset=UTF-8";
     Date = "Mon, 07 Sep 2015 01:19:29 GMT";
     Server = "Apache-Coyote/1.1";
     "Transfer-Encoding" = Identity;
     }
     2015-09-07 09:19:32.287 01-NSURLConnection基本使用[1116:143990] touches...即将结束-----
     */
}

/*============================get 异步方法发送request======================*/
- (void)sendAsync
{
    // 异步方法发送request
    // 1.创建URL
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
    // 2.创建request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 3.异步发送request
    /*
     参数说明:
     参数一:需要请求的对象;
     参数二:回调block的队列,决定了block在哪个线程中执行;
     参数三:回调block(响应体句柄)
     */
    // 注意:如果这里是同步,会阻塞当前线程
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        /*
         response:响应头;
         data:响应体;
         connectionError:错误信息;
         */
        NSLog(@"%@",request.HTTPBody);
        NSLog(@"%@",request.allHTTPHeaderFields);
        /*
         2015-09-07 09:59:20.422 01-NSURLConnection基本使用[1425:362793] (null)
         2015-09-07 09:59:20.423 01-NSURLConnection基本使用[1425:362793] (null)
         为啥是NULL??😖
         */
        NSLog(@"%@",response);
        NSLog(@"%@",[NSThread currentThread]);
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];



    NSLog(@"touches...即将结束-------");

    /*
     2015-09-07 09:15:42.689 01-NSURLConnection基本使用[1075:121037] touches...即将结束-------
     2015-09-07 09:15:42.731 01-NSURLConnection基本使用[1075:121037] <NSThread: 0x7fd649d19e20>{number = 1, name = main}
     2015-09-07 09:15:42.731 01-NSURLConnection基本使用[1075:121037] {"success":"登录成功"}
     */
}

下载请求

上一篇 下一篇

猜你喜欢

热点阅读