本来常用的是application/json的这中格式,做ios常用的网络请求框架AFNetworking,默认也是application/json,前不久就遇到这种接受格式问题,按照网上各种说法,改了AFNetworking的设置,最终还是无法解决问题,最后也放弃了使用AFNetworking。写了个原生的网络请求就解决了问题。
代码如下:
1 NSHTTPURLResponse *response = nil; 2 3 NSError *erro = nil; 4 5 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlStr]]; 6 7 request.HTTPMethod = @"POST"; 8 //设置格式 9 [request addValue:@"application/x-www-form-urlencoded ;charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 10 // 11 [request addValue:@"2" forHTTPHeaderField:@"x-wh-token"]; 12 //用HMAC加密 13 NSString *serkey = [NSString hmacsha1:@"" key:key]; 14 15 [request addValue:serkey forHTTPHeaderField:@"x-wh-signature"]; 16 17 [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; 18 19 NSData *requesData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&erro];
时间: 2024-10-01 06:34:00