1

私はASIHTTPRequestを使用して、次のようにフォームを送信しています。

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

nsstring fooのエンコーディングを設定するにはどうすればよいですか?

フォームデータを受信するWebは、ISOLatin1の値を期待しています

4

2 に答える 2

3

ASIFormDataRequests を使用すると、サーバーに送信されるコンテンツのエンコーディングを次のように設定できます。

[request setStringEncoding:NSISOLatin1StringEncoding].

デフォルトは NSUTF8StringEncoding です。

于 2011-03-23T22:08:20.310 に答える
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 のソースを見ると、このメソッドはPOST 値に-setPostValue:forKey:任意のサブクラスを使用しているように見えます。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"];
于 2009-09-26T20:41:39.810 に答える