我正在使用ASIHTTPRequest以这种方式发送表单:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:foo forKey:@"post_var"];
如何设置nsstring foo的编码??
接收表单数据的Web需要ISOLatin1中的值
答案 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"];