我正在编写需要与后端服务器通信的iPad应用程序。企业在使用这个后端的第一步是登录,并为这个服务器有一个我们可以张贴到一个URL,我这样做:iOS:来自一个呼叫的两个请求?
// Create the request.
NSString* loginURL = @"http://foo.local/signature/service/auth/rest/firewall/login";
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginURL]];
NSString* credentials = @"{\"userName\":\"[email protected]\", \"password\":\"password\"}";
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[credentials dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES]];
// Logging in...
NSError* error = nil;
NSURLResponse* response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
NSString* responseString = [NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]];
NSLog(@"Response String is: %@\n", responseString);
NSLog(@"Header fields are: %@\n", [httpResponse allHeaderFields]);
有什么奇怪的是,响应我得到是错误405:方法不允许。如果我正在做一个GET,我会预料到这一点,但我正在做一个POST。
我安装了WireShark来检查HTTP请求,看起来实际上有两个。第一个是POST调用,服务器返回一些cookie信息作为响应,然后第二个GET调用,这是上面的代码返回的。
为什么会发生这种情况?第一次与服务器的响应有关吗?
您是否正在重定向? – Wain
是的,这绝对是一种可能性,IP地址是不同的。我应该如何处理? – easythrees
你怎么没有使用AFNetworking或其他网络库? –