我正在研究一些代碼以連接到一個安全的基於.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