为什么 IOS 9 中的 NSURLSession 会产生错误 "NSPOSIXErrorDomain “Operation Cancelled"?
Why would an NSURLSession in IOS 9 generate error "NSPOSIXErrorDomain “Operation Cancelled"?
在 iOS 9 中,我正在使用 NSURLSession 并且对于一个特定的 URL,它生成了以下错误:
Error Domain=NSPOSIXErrorDomain Code=89 "Operation canceled".
配置 ATS 诊断,使用 nscurl --ats-diagnostics 当前 url returns 无错误。
还有其他人见过这种吗?
更新:
现在给出回应:
"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()
去掉Content-Typeheader中分号后的space。显然 NSURLSession 非常 对验证 header 很挑剔(因为 curl 请求在没有进行更改的情况下成功)。
并如前所述删除 Content-Length header。
在 iOS 9 中,我正在使用 NSURLSession 并且对于一个特定的 URL,它生成了以下错误:
Error Domain=NSPOSIXErrorDomain Code=89 "Operation canceled".
配置 ATS 诊断,使用 nscurl --ats-diagnostics 当前 url returns 无错误。
还有其他人见过这种吗?
更新:
现在给出回应:
"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()
去掉Content-Typeheader中分号后的space。显然 NSURLSession 非常 对验证 header 很挑剔(因为 curl 请求在没有进行更改的情况下成功)。
并如前所述删除 Content-Length header。