我正在尝试使用基本身份验证调用 https Web 服务(RESTful)。如果我将凭据放在 url 本身中,它可以正常工作,但我宁愿将其添加到请求中,这样密码就不会出现,例如在异常中。
我正在使用以下代码:
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];
但它不起作用。委托方法被调用,我可以在didReceiveAuthenticationChallenge
那里添加一个凭证,但理想情况下我会随请求一起发送它。
有任何想法吗?