The default encoding format of AFNetworking2.0 is UTF8. How to specify the parameter encoding format as gb2312
Q:
AFNetworking2.0 encodes parameters with UTF8. How can I change AFNetworking 2.0's parameter encoding to gb2312?
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
That encoding is gb2312, but how to add it to AFNetworking?
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; manager. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "text/html"]; NSDictionary * parameters =@{ @ "_ VIEWSTATE": @ "parameters % 2B % 2B ", @ "TextBox1": self. xueHao. text, @ "TextBox2": self. miMa. text, @ "TextBox3": self. yanZhengMa. text, @ "RadioButtonList1": @ "% D1 % A7 % C9 % FA"}; [manag Er POST: @ "http: // 172.21.96.64/default2.aspx" parameters: parameters success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@ "JSON: % @", responseObject); // submit the form} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "Error: % @",@"??? ") ;}] ;}
Response HeadersCache-Control:no-cache, no-storeContent-Length:5628Content-Type:text/html; charset=gb2312Date:Sun, 16 Feb 2014 14:00:14 GMTExpires:-1Pragma:no-cachePragma:no-cacheServer:Microsoft-IIS/6.0X-AspNet-Version:1.1.4322X-Powered-By:ASP.NET
A:
After digging around in the source code, it looks likeAFHTTPRequestOperationManager
Has a property for the request serializer-which then has a property for the string encoding.
So, you shoshould be able to do this:
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 % 2 B % 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); // submit the form} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "Error: % @",@" ??? ") ;}] ;}
Note that I haven't had a chance to test this yet, but from looking at the source code I'm pretty sure it will work. Confirmation wocould be appreciated.