ASIHTTPRequest:在后期数据中编码

时间:2009-09-26 16:09:57

标签: iphone encoding asihttprequest

我正在使用ASIHTTPRequest以这种方式发送表单:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:foo forKey:@"post_var"];

如何设置nsstring foo的编码??

接收表单数据的Web需要ISOLatin1中的值

2 个答案:

答案 0 :(得分:3)

ASIFormDataRequests允许您设置发送到服务器的内容的编码,如下所示:

[request setStringEncoding:NSISOLatin1StringEncoding].

默认为NSUTF8StringEncoding。

答案 1 :(得分:0)

const char *_cFoo = "bar";
NSString *_foo = [[NSString alloc] initWithCString:_cFoo encoding:NSISOLatin1StringEncoding];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:_foo forKey:@"post_var"];
// ...
[request trigger];
[_foo release];

编辑:我不确定为什么上述情况不起作用。我想我应该尝试一下。但是查看ASIHTTPRequest的源代码,-setPostValue:forKey:方法看起来像POST值的任何NSObject子类:

- (void)setPostValue:(id <NSObject>)value forKey:(NSString *)key
{
   if (![self postData]) {
       [self setPostData:[NSMutableDictionary dictionary]];
   }
   [[self postData] setValue:[value description] forKey:key];
   [self setRequestMethod:@"POST"];
}

也许将NSString转换为C字符串并使用其NSData表示形式作为POST变量值:

NSString *_foo = @"bar";
const char *_cFoo = [_foo cStringUsingEncoding:NSISOLatin1StringEncoding];
NSData *_cFooData = [NSData dataWithBytes:_cFoo length:strlen(_cFoo)];
[request setPostValue:_cFooData forKey:@"post_var"];