如何更改AFNetworking2.0参数编码?

时间:2014-02-16 14:07:50

标签: objective-c afnetworking afnetworking-2

AFNetworking2.0使用UTF8对参数进行编码。如何将AFNetworking 2.0的参数编码更改为gb2312?

NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000); 

该编码是gb2312,但如何将其添加到AFNetworking?

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{@"__VIEWSTATE":@"dDwtMTg3MTM5OTI5MTs7PkDQD2kYQWAxp4gTWKdd1YunUJ%2B%2B",@"TextBox1": self.xueHao.text,@"TextBox2":self.miMa.text,@"TextBox3":self.yanZhengMa.text,@"RadioButtonList1":@"%D1%A7%C9%FA"};
[manager POST:@"http://172.21.96.64/default2.aspx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);//提交表单

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", @"???");
}];  }

Response Headers
Cache-Control:no-cache, no-store
Content-Length:5628
Content-Type:text/html; charset=gb2312
Date:Sun, 16 Feb 2014 14:00:14 GMT
Expires:-1
Pragma:no-cache
Pragma:no-cache
Server:Microsoft-IIS/6.0
X-AspNet-Version:1.1.4322
X-Powered-By:ASP.NET

1 个答案:

答案 0 :(得分:2)

the source code中进行挖掘后,请求序列化程序看起来像AFHTTPRequestOperationManager has a property,然后has a property进行字符串编码。

所以,你应该能够做到这一点:

NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);

RequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer.stringEncoding = enc;
NSDictionary *parameters = @{@"__VIEWSTATE":@"dDwtMTg3MTM5OTI5MTs7PkDQD2kYQWAxp4gTWKdd1YunUJ%2B%2B",@"TextBox1": self.xueHao.text,@"TextBox2":self.miMa.text,@"TextBox3":self.yanZhengMa.text,@"RadioButtonList1":@"%D1%A7%C9%FA"};
[manager POST:@"http://172.21.96.64/default2.aspx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);//提交表单

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", @"???");
}];  }

请注意,我还没有机会测试这个,但是从查看源代码我很确定它会起作用。确认将不胜感激。