Rub*_*Jr. 22 networking ios swift ios8
我正在尝试使用Swift发出SOAP请求.从9/9/14开始运行最新的Xcode/iOS.我使用的NSMutableURLRequest
是我将HTTPBody添加到请求信息中.但是,一旦我启动NSURLConnection
了请求,我收到一个错误"Stream xxxxxxxxx在打开之前发送一个事件".我没有使用任何网络库,只是一个普通的老库NSURLConnection
.有什么可能导致这个错误的想法?谢谢!
使用中的相关代码:
func createSOAPRequestWithEnvelope(soapEnvelope : String) {
//create request
var url = NSURL(string: "https://my-service-url")
var req = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 5000)
req.addValue("text/xml", forHTTPHeaderField: "Content-Type")
req.HTTPMethod = "POST"
req.HTTPBody = soapEnvelope.dataUsingEncoding(NSUTF8StringEncoding)
//begin connection
var connection = NSURLConnection(request: req, delegate: self, startImmediately: false)
connection.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
connection.start() //error happens after this command :(
}
//takes care of NTLM Authentication
func connection(connection: NSURLConnection!, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge!) {
var authMethod = challenge.protectionSpace.authenticationMethod
if authMethod == NSURLAuthenticationMethodNTLM {
var credential = NSURLCredential(user: self.username,
password: self.password,
persistence: NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
}
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
// Response received, clear out data
self.transactionData = NSMutableData()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
// Store received data
self.transactionData?.appendData(data)
}
Run Code Online (Sandbox Code Playgroud)
小智 4
Stream not Open 的问题似乎直接是 CFNetwork 实现的问题。(使用 NSConnection 和 NSSession 时可以重现)。它发生在需要分块的请求(响应)上。此问题的一个副作用是 Windows 身份验证无法正常工作,每个请求都需要经过完整的握手过程。iOS 8.1 中也没有修复。