1

可能重复:
我可以使用 NSURLCredentialStorage 进行 HTTP 基本身份验证吗?

我将如何接受自签名服务器证书?使用下面的代码,我只能在使用 Safari 接受服务器证书后进行连接/身份验证。

- (void)secure:(NSString *)username credentials:(NSString *)login
{
  NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:username
                                                                password:login
                                                             persistence:
                                                        NSURLCredentialPersistenceForSession];

  NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:LIST_URL_HTTPS
                                                                      port:443
                                                                   protocol:@"https"
                                                                     realm:@"Restricted Area"
                                                      authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

  [[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
                                               forProtectionSpace:space];


  NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageURL]
                                                            cachePolicy:NSURLCacheStorageNotAllowed
                                                        timeoutInterval:30];

  NSURLResponse *response;
  NSError *error;

  NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest
                                             returningResponse:&response
                                                         error:&error];

}
4

0 回答 0