如何在AFNetworking iOS中传递非英文字符请求?

时间:2017-08-03 01:56:40

标签: ios objective-c afnetworking-3

如何在AFNetworking iOS中编码/设置UTF-8非英文字符(例如中文字符)?

我的代码:

    NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:self.httpManager.responseSerializer.acceptableContentTypes];
    [contentTypes addObject:@"text/plain"];
    [contentTypes addObject:@"text/html"];
    [contentTypes addObject:@"application/x-www-form-urlencoded"];
    [contentTypes addObject:@"charset=UTF-8"];
    [contentTypes addObject:@"application/json"];

    self.httpManager.responseSerializer.acceptableContentTypes =
    [NSSet setWithArray:@[@"text/plain",
                          @"text/html",
                          @"application/x-www-form-urlencoded",
                          @"charset=UTF-8",
                          @"application/json"]];

后端会得到类似的东西

  

AAEA

而不是正确的字符。

2 个答案:

答案 0 :(得分:1)

最后这是解决方案:

[self.httpManager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

答案 1 :(得分:0)

我的申请中有一个问题。以下是我的代码,希望它能帮到你。

NSData *data = [strStringWithUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
if(goodValue == nil)
    return strStringWithUnicode;
return goodValue;