2013-01-13 143 views
1

我想在我的iOS应用程序中执行POST请求。在iOS中发布请求?

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

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"[email protected]","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\":\"%@\"}",@"[email protected]",@"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]; 

我缺少/做错了吗?

回答

1

想通了:

我只是需要加入这一行:

[request setHTTPShouldHandleCookies:NO];