·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 数据请求

数据请求

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

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) {

        

    }];