我正在嘗試使用基本身份驗證來調用https Web服務(RESTful)。 它工作正常,如果我把憑據放在url本身,但我寧願將它添加到請求,以便密碼不會出現,例如在一個異常。iPhone:如何使用NSURLCredential獲取對HTTPS Web服務的基本身份驗證
我使用下面的代碼:
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"myuser"
password:@"mypassword"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"example.com"
port:443
protocol:@"https"
realm:nil
authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
,但它不工作。代理方法被調用,我可以在那裏添加一個憑證,但理想情況下,我會將它與請求一起發送。
任何想法?
我以前試過,但無法讓它工作。我會再給它一次。 – Ian1971 2010-03-19 16:09:28
它會給你一個錯誤?你確定錯誤是因爲驗證質詢失敗嗎? – lostInTransit 2010-03-20 04:49:23
這對我有用,奇怪的是蘋果文檔中沒有提到它。此外,我不得不推出我自己的Base64編碼,這是非常可怕的。 – 2012-05-29 16:16:31