【问题标题】:handle sendasynchronousrequest with HTTPS in IOS?在 IOS 中使用 HTTPS 处理 sendasynchronousrequest?
【发布时间】:2014-01-08 07:19:36
【问题描述】:

我有一个 HTTPS 请求。

我正在使用NSURLConnectionsendAsynchronousRequest。我不想使用

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

当我使用NSURLConnectionsendAsynchronousRequest 时,每次我都会收到以下错误

 NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9806)
 CFNetwork SSLHandshake failed (-9806)

这是我的代码

NSURL *url=[NSURL URLWithString:strService];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSString *msgLength = [NSString stringWithFormat:@"%d", [strSoap length]];

[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];;

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

[theRequest setValue:@"My User-Agent" forHTTPHeaderField:@"User-Agent"];
[theRequest addValue:strSoapAction forHTTPHeaderField:@"SOAPAction"];
[theRequest setHTTPBody: [strSoap dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:TRUE]];

[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *resp, NSData *responseData, NSError *error) {
    NSString *strResponse=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;


    strResponse=[self clearHtmlTags:strResponse];

    NSMutableDictionary *dictResponse=[NSMutableDictionary dictionaryWithDictionary:[NSDictionary dictionaryWithXMLString:strResponse]];

    block(dictResponse);
}];

这是用 sendAsynchronousRequest 调用 NSURLConnection 的委托方法的任何其他方式吗?

【问题讨论】:

    标签: ios http-post nsurlconnection


    【解决方案1】:

    尝试添加此委托方法。希望它对你有用。

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    
            // Verify certificate:
            SecTrustResultType trustResult;
            OSStatus status = SecTrustEvaluate(challenge.protectionSpace.serverTrust, &trustResult);
            BOOL trusted = (status == errSecSuccess) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));
    
            if (trusted) {
                [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
                     forAuthenticationChallenge:challenge];
            } else {
                if (1) {
                    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
                         forAuthenticationChallenge:challenge];
                } else {
                    [challenge.sender cancelAuthenticationChallenge:challenge];
                }
            }
        } else {
            [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
        }
    }
    

    【讨论】:

    • 但是我应该在哪里设置委托?
    • 这是一个不会被调用的委托方法。
    • 为什么不调用?您需要在 .h 文件中添加协议NSURLConnectionDataDelegate
    • 当我在此处为 sendAsynchronousRequest 设置委托时会调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2015-02-25
    • 2013-04-21
    相关资源
    最近更新 更多