2

我是objective-c的新手,想知道是否有更好的方法来创建我下面的方法的HTTP POST主体。

- (NSData*) generateBody: (NSDictionary*) requestParams {
NSMutableData *body = [[[NSMutableData alloc] initWithCapacity:1024] autorelease]; 

BOOL firstParam = YES;

for (NSString* key in requestParams) {
    NSString *value = [requestParams valueForKey:key];

    if ( firstParam ) {
        firstParam = NO;
    } else {
        [body appendData:[@"&" dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[key dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"=" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}

return body;

}

4

2 回答 2

0

Yes.

Have a look at ASIHTTPRequest , specifically, the ASIFormDataRequest class (just search for it in that link)

It's like a better version of NSURLConnection :) It also seems to be becoming the industry standard way to do web requests and an essential part of every iOS developers library of tools :)

于 2011-01-20T12:11:43.707 回答
0

如果它接受 JSON 格式的数据,请尝试查看 Apple 文档中的 NSJSONSerialization。效果很好。

于 2013-04-10T20:02:10.423 回答