我正在编写一个需要与后端服务器通信的 iPad 应用程序。问题是我得到的代码失败了,我不明白为什么。当我使用“curl”时,我得到了预期的响应:
这是卷曲的用法:
curl -X POST -H "Content-Type: application/json;charset=UTF-8" -d '{"userName":"foo2@foolinator.com", "password":"password"}' "http://server.local/signature/service/auth/rest/firewall/login" --dump-header headers
当我打开“标题”文件时,我得到了我所期望的 302 响应,其中包含 cookie 中的信息。但是,我的 Objective C 代码得到了“内部服务器错误”的响应:
// Create the request.
NSString* loginURL = @"http://server.local/signature/service/auth/rest/firewall/login";
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginURL]];
NSString* credentials = @"'{\"userName\":\"foo2@foolinator.com\", \"password\":\"password\"}'";
NSData* connectionData = [credentials dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:patientConnectionData];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
// Logging in...
NSError* error = nil;
NSURLResponse* response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
NSString* mime = [httpResponse MIMEType];
NSString* responseString = [NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]];
响应是错误 500:内部服务器错误。我究竟做错了什么?