我正在研究一些代码以连接到一个安全的基于.NET的Web服务。我尝试使用手动创建的SOAP消息,HTTP POST和HTTP GET进行连接。在每次尝试中,我在NSURLConnection上使用NSMutableURLRequest。我通过以下方式实施了didReceiveAuthenticationChallenge ...iOS 4.3 - 尝试连接Web服务时网络连接丢失?
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Authentication challenge");
if ([challenge previousFailureCount] == 0)
{
NSLog(@"authentication failed");
NSURLCredential *credential ;
credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
NSLog(@"credential sent");
}
else
{
NSLog(@"Credentials are wrong!");
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
所有日志消息出现。在“发送凭证”后大约30秒后,出现以下错误。
连接失败:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。”
我在这里和其他地方发现了很多处理认证挑战的例子。我所做的事似乎与众不同。为了以防万一,我还实施了以下措施,但结果相同。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
NSLog(@"canAuthenticateAgainstProtectionSpace");
//return YES to say that we have the necessary credentials to access the requested resource
return YES;
}
我发送的用户名/密码是为了回应挑战而存在。我已通过在浏览器中输入服务URL并在验证对话框中输入用户名/密码进行了验证。在通过浏览器调用服务时返回XML。
现在我已经把头撞在墙上了几天了。任何指针将不胜感激。
感谢
肖恩
我只是想通过只注释掉它,只是放置在URL中的用户名/密码,使得不didReceiveAuthenticationChallenge连接的“http://用户名:密码@服务器/路径/要/服务”。仍然收到相同的错误。 – Sean 2011-03-25 21:48:44
我也有同样的问题,肖恩你解决了你的问题,如果是的话请帮我解决这个问题。尽快地。 – Ankur 2012-09-12 10:43:50