zoukankan      html  css  js  c++  java
  • NSURLSession请求网络-01-网络

        /*

         网络请求的流程:

         1.构造NSURL连接地址

         2.构造NSURLRequest请求对象,包含请求头和请求体信息。

         3.构造NSURLSessionConfiguration,可选

         4.构造NSURLSession会话对象

         5.创建请求任务

         6.发送网络请求

         

         */

      1 //
      2 //  ViewController.m
      3 //  01-NSURLSession请求网络
      4 //
      5 //
      6 
      7 #import "ViewController.h"
      8 
      9 @interface ViewController ()
     10 
     11 @end
     12 
     13 @implementation ViewController
     14 
     15 - (void)viewDidLoad {
     16     [super viewDidLoad];
     17 
     18 }
     19 
     20 - (IBAction)getAction:(id)sender {
     21     
     22     /*
     23      网络请求的流程:
     24      1.构造NSURL连接地址
     25      2.构造NSURLRequest请求对象,包含请求头和请求体信息。
     26      3.构造NSURLSessionConfiguration,可选
     27      4.构造NSURLSession会话对象
     28      5.创建请求任务
     29      6.发送网络请求
     30      
     31      */
     32     
     33     NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"];
     34     
     35     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     36     
     37     request.timeoutInterval = 15;
     38     request.HTTPMethod = @"GET";
     39     //请求头的设置
     40 //    request.allHTTPHeaderFields = @{
     41 //                                    
     42 //                                    };
     43 //    request setValue:<#(nullable NSString *)#> forHTTPHeaderField:<#(nonnull NSString *)#>
     44     
     45     NSURLSession *session = [NSURLSession sharedSession];
     46     
     47 
     48     NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
     49         //网络请求完成,获取响应数据后会回调的block
     50         //data表示获取的数据,response表示请求对象,error表示请求错误
     51         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
     52         //获取状态码
     53         NSLog(@"statusCode:%li", httpResponse.statusCode);
     54         
     55         //获取响应头
     56         NSDictionary *responseHeader = httpResponse.allHeaderFields;
     57         NSLog(@"responseHeader:%@", responseHeader);
     58 
     59         NSError *jsonError = nil;
     60         
     61         NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
     62         
     63         if (!jsonError) {
     64             
     65             //显示我们的数据,如果要更新UI则需要回到主线程完成。
     66             NSLog(@"%@", result);
     67             
     68         }
     69         
     70         
     71         
     72     }];
     73     
     74     //发送网络请求
     75     [task resume];
     76     
     77     
     78 }
     79 
     80 - (IBAction)postAction:(id)sender {
     81     
     82     NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/cinema/schedule.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"];
     83     
     84     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     85     
     86     request.timeoutInterval = 15;
     87     request.HTTPMethod = @"POST";
     88     
     89     NSString *bodyString = @"cinema_id=1533";
     90     NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
     91     //POST请求的请求参数加在请求体中。
     92     request.HTTPBody = bodyData;
     93     
     94     NSURLSession *session = [NSURLSession sharedSession];
     95     
     96     
     97     NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
     98         
     99     
    100         NSError *jsonError = nil;
    101         
    102         NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
    103         
    104         if (!jsonError) {
    105             
    106             //显示我们的数据,如果要更新UI则需要回到主线程完成。
    107             NSLog(@"%@", result);
    108             
    109         }
    110         
    111         
    112     }];
    113     
    114     [task resume];
    115     
    116 }
    117 @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    区别Lua模式匹配中 %a+ 与 .-
    将硬件规定的通信协议用Lua实现(涉及到很多Lua通信的数据转换)
    Lua库-string库
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
    Unity3d
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5433046.html
Copyright © 2011-2022 走看看