1

我正在尝试在我的 iOS 应用程序中执行 POST 请求。

以下行在我的 Mac 的命令行中按预期工作

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"frank.smith@gmail.com","password":"frankSmith"}' http://server.address

但是,我似乎无法将其正确转换为 iOS。这是我尝试过的:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",SERVER_ADDRESS]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *messageBody = [NSString stringWithFormat:@"{\"email\":\"%@\",\"password\":\"%@\"}",@"frank.smith@gmail.com",@"frankSmith"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];
[connection start];

我错过了什么/做错了什么?

4

1 回答 1

1

弄清楚了:

我只需要添加这一行:

[request setHTTPShouldHandleCookies:NO];

于 2013-01-13T02:52:14.183 回答