NSURLConnection 发送https 协议正常收到response

来源:互联网 发布:淘宝网店铺装修模板 编辑:程序博客网 时间:2024/05/01 14:44

用NSURLConnection 进行send https request 时,有两种方法可以 解决 收到response 的问题:

第一种 方法 是使用 NSURLRequest 的私有API

@implementation NSURLRequest(SSL)

+(BOOL) allowsAnyHTTPSCertificateForHost:(NSString*)host

{

    return YES;

}

@end


第二种方法 是 实现  NSURLConnectionDataDelegate 中的两个方法

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

{

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])

    {

        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    }

    else

    {

        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];

    }

}



原创粉丝点击