调用HTTPS Soap服务时出现NSURLConnection错误

时间:2016-02-08 06:46:00

标签: ios web-services soap https nsurlconnection

我正在尝试从Soap请求中获取数据,但是我的应用程序因此错误而崩溃:

2016-02-08 11:12:11.524 Example[567:128898] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
Response: nil
fatal error: unexpectedly found nil while unwrapping an Optional value

我已在plist中启用安全设置并成功调用其他服务。

注意:服务是在HTTPS上,我是否因此而收到此错误?这需要任何证书吗?

我是第一次拨打肥皂服务,请指导我如何在服务中添加电子邮件参数:(https://[IP]:9897/JLIPolicyinfo.asmx?op=GetEmail_Poldetail

这是我从这里得到的代码:(IOS Swift Call Web Service using SOAP

        let is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"

        let is_URL: String = "https://58.27.241.203:9897/JLIPolicyinfo.asmx"

        let lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
        let session = NSURLSession.sharedSession()
        let err: NSError?

        lobj_Request.HTTPMethod = "POST"
        lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
        lobj_Request.addValue("58.27.241.203", forHTTPHeaderField: "Host")
        lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        lobj_Request.addValue(String((is_SoapMessage.characters.count)), forHTTPHeaderField: "Content-Length")
        //lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
        lobj_Request.addValue("http://tempuri.org/GetEmail_Poldetail", forHTTPHeaderField: "SOAPAction")

        let task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Body: \(strData)")

            if error != nil
            {
                print("Error: " + error!.description)
            }

        })
        task.resume()

这是我的补充说明:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>

    <key>NSExceptionDomains</key>
    <dict>
        <key>https://58.27.241.203:9897/JLIPolicyinfo.asmx</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <false/>
            <key>NSExceptionAllowInsecureHTTPSLoads</key>
            <false/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

1 个答案:

答案 0 :(得分:1)

您可能拥有自签名证书也可能是个问题。

<强>解决方案: - 如果您续订服务器的ssl证书以使用SHA2并启用TLS v1.2,它将起作用。

可能是一个解决方案: -  您必须在info.plist文件中添加它,以便它可以允许非安全连接: -

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

甚至你可以这样添加: -

enter image description here

修改: -

在info.plist中添加此内容以获取特定异常。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <false/>
            <key>NSExceptionAllowInsecureHTTPSLoads</key>
            <false/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>