选用 get 与 post 的一些建议

1、http的请求方法:get  post

 

2、 get:会把请求的内容  放到链接地址里面(数据请求的时候  默认的是get请求)

        例:www.baidu.com/user/login?username=刘水,psw=123  (密码暴露在外面)

  2.1、 get特征:

          1、浏览器和服务器对URL长度有限制,因此在URL后面附带的参数是有限制的,通常不能超过1kB

           2、会把请求的数据 暴露在接口里面

 

3、post: 参数全部放在请求体中

      这样就保证了  数据的安全

      没有具体的长度限制(唯一的限制 就是 服务器的承受能力)

 

4、选择get 和 post的建议:

    4.1、如果要传递大量数据,比如文件上传,只能用post请求

    4.2、get的安全性比post要差一些,如果包含机密\敏感信息,建议使用post

    4.3、如果仅仅是索取数据(数据查询),建议使用get

    4.4、如果是增加、修改、删除数据,建议使用post

 接口:

     通过id获取用户信息:host/user/info

     id = @""

 

5、URL:Uniform Resource Locator(统一资源定位符)

 通过URL,能找到互联网上唯一的一个资源

 

6、网络请求:同步请求   异步请求

    6.1、同步请求:等所有操作完全执行完毕 才会继续执行

         同步请求的缺陷:会遇到 假死现象(只要请求的操作没有执行完毕就不会再去响应任何事件(在同一线程))

    6.2、异步请求:在程序运行的时候,会利用空闲的时间,去执行里面的操作;不会影响到同一线程里面的其他操作

//   get 获取方法

    NSString *interfaceString = @"http://apis.baidu.com/showapi_open_bus/mobile/find";
    NSString *requestContentString = @"num=18798810755";
    
    NSString *urlString = [NSString stringWithFormat:@"%@?%@", interfaceString, requestContentString];
    
//    把链接地址字符串 转换成 NSUTF8StringEncoding
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
//    可变请求 可以添加 请求方式  以及请求的 请求头 或者更多
//    timeoutInterval 请求所需时间  超过 时间  不再发送这个请求
//    cachePolicy 缓存内容的方式
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//    指定http的请求方式
    request.HTTPMethod = @"GET";  // 字符一定要大写
     NSString *apiKey = @"apiKey";
    
//    把apikey 发送给服务器指定的请求头 位置
//    forHTTPHeaderField 需要的KEY  是服务器指定的key
    [request addValue:apiKey forHTTPHeaderField:@"apiKey"];
    
//    用异步发送
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[[NSOperationQueue alloc]init]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data,
                                               NSError *connectionError) {
        
        NSLog(@"%@", response);
        
//        解析 json 文件
        
//        把data 转换成 json 文件  (这个方法可以在网上找到第三方方法)
//        这里的之所以用字典接受 是因为进入 JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil 方法后查看到它是id类型
//        NSLog(@"data~ %@", data);
        NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"解析数据:%@", info);
        NSLog(@"%@ %@ %@", info[@"showapi_res_body"][@"prov"], info[@"showapi_res_body"][@"city"], info[@"showapi_res_body"][@"name"]);
        
        
    }];

 

//  post 获取方式

 NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetService"];
    
    NSDictionary *dic = @{@"PlatformType":@"3"};
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:10];
    
//    设置HTTP请求的方式
    request.HTTPMethod = @"POST";
    
//    设置 请求的参数
//    HTTPBody 要的是data
//    dataUsingEncoding 把字符串 转成data
    request.HTTPBody = [[NSString stringWithFormat:@"%@", dic] dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"info~%@", info);
        NSURL *url = [NSURL URLWithString:info[@"data"][@"adUrl"]];
        
        //    (NSURLRequest 请求)
        //  实例化 请求对象 里面携带着 请求的地址
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        //   需要 通过 连接 异步发送(Asynchronous 异步) 请求
        //    线程
        NSOperationQueue *queue = [[NSOperationQueue alloc]init];
        
        //    发送一个异步请求 在queue 这个线程里面去执行
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data1, NSError *connectionError) {
            
            //        response 是服务器  回应的内容(回应状态的code 以及错误信息error)
            //        data 是回应给客户端 需要的数据
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
            imageView.image = [UIImage imageWithData:data1];
            [self.view addSubview:imageView];

    }];
    
    
    }];

 

posted @ 2015-09-21 22:16  8023博客  阅读(426)  评论(0编辑  收藏  举报