调用HTTPS Soap服务时出现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 )

这里是我从这里得到的代码:( 使用SOAP的IOS Swift调用Web服务 )

  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() 

这是我的plist补充:

 <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> 

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

解决scheme: –如果您续订您的服务器的ssl证书以使用SHA2并启用TLS v1.2,则它将起作用。

可能是一个解决scheme: –你必须在你的info.plist文件中添加它,以便它可以允许不安全的连接: –

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

甚至你可以像这样添加:

在这里输入图像说明

编辑:-

在你的info.plist中添加这个特定的exception。

 <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>