为什么IOS 9中的NSURLSession会生成错误“NSPOSIXErrorDomain”操作已取消“?

时间:2016-09-06 18:21:07

标签: ios ios9 nsurlsession

在iOS 9中,我使用的是NSURLSession,对于一个特定的URL,它会产生以下错误:

Error Domain=NSPOSIXErrorDomain Code=89 "Operation canceled".

使用nscurl --ats-diagnostics为当前url配置ATS诊断会返回nil错误。

其他人见过这种?

更新的 现在给出回应:   
“curl --head soap.alfabank.kz:30444/soa/rate?wsdl”

HTTP/1.1 405 Method Not Allowed Content-Type: application/soap+xml; 
charset=UTF-8 Content-Length: 286 
Server: Jetty(8.1.14.v20131031) 
Strict-Transport-Security: max-age=157680000 
X-FRAME-OPTIONS: DENY

一些代码:

let url = "https://soap.alfabank.kz:30444/soa/rate?wsdl"
let soapMessage = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><GetRates xmlns=\"http://soa.rate.alfabank.kz/\"><RateListRq reqid=\"6f56631b-01d0-4886-917b-1c5565743627\" systemcode=\"VSTest\"><list_type xmlns=\"\">short</list_type><period xmlns=\"\"><fromDate>2016-09-07</fromDate><toDate>2016-09-07</toDate></period></RateListRq></GetRates></s:Body></s:Envelope>"
let request = NSMutableURLRequest(URL: NSURL(string:url)!)
let msgLength = soapMessage.characters.count
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(String(msgLength), forHTTPHeaderField: "Content-Length")
request.HTTPMethod = "POST"
request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithRequest(request){
    data, response, error in
       if error != nil {
           print(error)
       }
      else {
//some            
      }
   }
task.resume()

1 个答案:

答案 0 :(得分:0)

删除Content-Type标头中分号后面的空格。显然 NSURLSession 非常挑剔验证该标题(因为卷曲请求成功而没有进行更改)。

并删除Content-Length标头,如前所述。