The default encoding format of AFNetworking2.0 is UTF8. How to specify the parameter encoding format as gb2312

Source: Internet
Author: User

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 likeAFHTTPRequestOperationManagerHas 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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.