使用Accept-Encoding=gzip头的请求 设置head和body


最近post一个接口,有请求头和body;一个请求头为 Accept-Encoding=gzip;

从paw模拟发送可以,试了好多次就是不能使用代码获取正确数据,

尝试新版paw后才知道代码是这么写滴...

以前使用后面的方法都可以正常获取,head里添加了"gzip" value就不行了

错误的数据:(可以看到没有value,都在name里面)

使用Accept-Encoding=gzip头的请求 设置head和body_第1张图片

正确的:

使用Accept-Encoding=gzip头的请求 设置head和body_第2张图片


成功获取数据:

- (void)sendRequest:(id)sender
{
    // My API (POST http://xxx.ooo.com/xx/)
    
    // Create manager
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // Form URL-Encoded Body
    NSDictionary* bodyParameters = @{
                                     @"product_id":@"17290973"
                                     };
    
    NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://xxx.ooo.com/xx/" parameters:bodyParameters error:NULL];
    
    // Add Headers
    [request setValue:@"182" forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"xxx.ooo.com" forHTTPHeaderField:@"Host"];
    [request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
    
    // Fetch Request
    AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"HTTP Response Status Code: %ld", [operation.response statusCode]);
            NSLog(@"HTTP Response Body: %@", responseObject);
            NSLog(@"%@",responseObject[@"status"]);
            NSLog(@"%@",responseObject[@"reason"]);
        
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"HTTP Request failed: %@", error);
        }];
    
    [manager.operationQueue addOperation:operation];
}


以前用的代码:(没有获取到正确数据)

NSDictionary * postDict = @{
                              @"product_id":@"17290973"
                                     };

    AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type" ];
    [manager.requestSerializer setValue:@"xxx.ooo.com" forHTTPHeaderField:@"Host" ];
    [manager.requestSerializer setValue:@"keep-alive" forHTTPHeaderField:@"Connection" ];
    [manager.requestSerializer setValue:@"182"  forHTTPHeaderField:@"Content-Length" ];
    [manager.requestSerializer setValue:@"gzip"  forHTTPHeaderField:@"Accept-Encoding" ];
    
    [manager POST:CartAdd parameters:postDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(error.localizedDescription);
    }];


你可能感兴趣的:(ios,网络请求)