iPhone - 具有自签名证书的服务器的HTTPS连接

时间:2009-03-24 19:11:48

标签: iphone certificate self signed

  

可能重复:
  Can I use NSURLCredentialStorage for HTTP Basic Authentication?

我如何接受自签名服务器证书?使用下面的代码,我只能在使用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];

}

0 个答案:

没有答案
相关问题