zoukankan      html  css  js  c++  java
  • 数据请求

    1.Get请求

    //get把传输的数据 放在链接地址里面
    - (void)getRequest{
        NSString *interfaceString = @"http://apis.baidu.com/showapi_open_bus/mobile/find";
        NSString *requestContentString = @"num=15761672938";
        
        NSString *urlString = [NSString stringWithFormat:@"%@?%@",interfaceString,requestContentString];
        NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    //    可变请求 可以添加请求方式 以及请求的 请求头 或者更多
    //    timeoutInterval请求所需时间 超过所需时间 超过时间不再发送这个请求
    //    cachePolicy缓存内容的方式
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
        NSString *apiKey = @"e7f5ac9e7c42a6c8cb125ee1d7e8779e";
    //    把apiKey 发送给服务器 指定的请求 位置
    //    forHTTPHeaderField需要的Key 是服务器指定的key
        [request addValue:apiKey forHTTPHeaderField:@"apiKey"];
        
        
    //    指定http的请求方式
        request.HTTPMethod = @"GET";//一般都是大写
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSLog(@"%@",response);
            //    解析json文件
            //    把data转换成json文件
            NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@",info);
            NSLog(@"info===%@   ,%@%@",info[@"showapi_res_body"][@"name"],info[@"showapi_res_body"][@"prov"],info[@"showapi_res_body"][@"city"]);
        }];
    }
    

    2.Post请求

    //post
    - (void)postRequest{
        NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetService"];
    //    请求参数
    //    PlatformType设备类型 3表示ios设备
        NSDictionary *dic = @{@"PlatformType":@"3"};
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //    设置请求方式
        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);
        }];
        
    }
    

     3.HTTP  Get请求

     GET URL 字符串地址 parameters boby体的内容

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        [manager GET:@"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    //        responseObject请求下来的数据内容

            

        } failure:^(AFHTTPRequestOperation *operation, NSError * error) {

            

        }];

    4.HTTP Post请求

     //post    parameters需要post的内容

        [manager POST:@"" parameters:@{} success:^(AFHTTPRequestOperation * operation, id responseObject) {

            

        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {

            

        }];

      

  • 相关阅读:
    js:值类型/引用类型/内存回收/函数传值
    JS学习计划
    起点
    哈夫曼压缩/解压缩(控制台,C++)
    二维数组作为函数参数传递(C++)
    二级指针和指针引用函数传参(C++)
    学生管理系统(C++,控制台,文件读取,姓名排序)
    C++的getline()和get()函数
    二叉排序树节点的删除(C++,算法导论),前中后序遍历(递归/非递归,栈实现),按层次遍历(队列实现)
    QT程序打包成EXE
  • 原文地址:https://www.cnblogs.com/fshmjl/p/4876626.html
Copyright © 2011-2022 走看看