NSURLSession请求 加gb2312编码

需求是:请求的时候参数是汉字的进行gb2312编码,

最开始用的AF,AF默认是UTF-8编码

通过修改编码格式不行, AF最后提交的时候会进行2次编码,

1.jpg 2.jpg

最后用原生写解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
NSStringEncoding encGbk = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
// NSString *retStr =name;
NSString *retStr = [name stringByAddingPercentEscapesUsingEncoding:encGbk];
NSString * urls = @"";//地址
// NSDictionary * dic = @{
// @"id_card":id_card,
// @"name":retStr,
// @"valCode":valCode,
// @"certificate_code":@"",
// @"evelop_code":@""
// };
//certificate_code=&evelop_code=&id_card=&name=%E5%90%95%E6%85%A7%E5%BC%BA&valCode=4174
NSString *body= [NSString stringWithFormat:@"certificate_code=%@&evelop_code=%@&id_card=%@&name=%@&valCode=%@",@"",@"",id_card,retStr,valCode];
NSURLSession *session=[NSURLSession sharedSession];
NSURL *url=[NSURL URLWithString:urls];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded;charset=gb2312" forHTTPHeaderField:@"Content-Type"];
request.timeoutInterval = 180;
[request setValue:@"iir.circ.gov.cn" forHTTPHeaderField:@"Host"];
[request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
[request setValue:@"gzip,deflate" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"zh-CN,zh;q=0.8" forHTTPHeaderField:@"Accept-Language"];
[request setValue:@"" forHTTPHeaderField:@"Origin"];//
[request setValue:@"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"" forHTTPHeaderField:@"Referer"];//
NSString * cookie = [[NSUserDefaults standardUserDefaults] valueForKey:@"cookie"];
NSLog(@"***********************cookie为 %@ *************************",cookie);
[request setValue:cookie forHTTPHeaderField:@"Cookie"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
// request.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/xml", @"text/plain", @"application/javascript", @"image/*",@"application/json", nil];
// request.HTTPBody = [[dic mj_JSONString] dataUsingEncoding:encGbk];
request.HTTPBody = [body dataUsingEncoding:encGbk];
NSString *str = [[NSString alloc] initWithData:request.HTTPBody encoding:encGbk];
NSLog(@" ***********************请求参数***********************%@",str);
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
if (error ==nil) {
NSString *str = [[NSString alloc] initWithData:data encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
VMLog(@" ***********************请求成功***********************%@---------------error===%@",str,error);
}else{
VMLog(@"%@",error);
}
}];
//7.执行任务
[dataTask resume];