我想知道服务器是否应该有特殊设置来使用 nsurlconnections?当我尝试创建连接和身份验证时。我没有收到任何错误或警告,但它没有连接。
有人可以向我解释一下这一点吗?
谢谢
编辑:这是我当前使用的代码
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"loginURl"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[webView1 scalesPageToFit];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Connection Failed");
}
}
最佳答案
不,您的服务器不需要任何特殊配置。 NSURLConnection
仅使用简单的 HTTP 请求。
对于身份验证:您需要自己实现身份验证所需的委托(delegate)方法。另请检查您是否通过 SSL 连接并为此实现所需的委托(delegate)方法。
编辑:这是我的一个使用 HTTP 身份验证的应用程序的一部分。我发现您还需要在 canAuthenticateAgainstProtectionSpace:
方法中为 NSURLAuthenticationMethodHTTPBasic 返回 YES,否则它将无法工作。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic];
}
关于iphone - nsurlconnection 服务器需要特殊设置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948903/