问题描述
我正在尝试使用Swift发出SOAP请求。从9/9/14开始运行最新的Xcode / iOS。我使用 NSMutableURLRequest
我将HTTPBody添加到请求信息中。但是,一旦我使用请求启动 NSURLConnection
,我会收到错误Stream xxxxxxxxx在打开之前发送事件。我没有使用任何网络库,只是一个普通的 NSURLConnection
。有什么可能导致这个错误的想法?谢谢!
I'm trying to make a SOAP request with Swift. Running the latest Xcode/iOS as of 9/9/14. I use an NSMutableURLRequest
that I add an HTTPBody to with the request info. However, Once I start an NSURLConnection
with the request, I get an error "Stream xxxxxxxxx is sending an event before being opened". I'm not using any networking libraries, just a plain old NSURLConnection
. Any thoughts on what could cause this error? Thanks!
正在使用的相关代码:
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)
}
推荐答案
Stream not Open的问题似乎直接是我的CFNetwork实现。 (使用NSConnection和NSSession时可重现)。它发生在需要分块的请求(响应)上。此问题的副作用是Windows身份验证无法正常工作,并且每个请求都需要经过完整的握手过程。它在iOS 8.1中也没有修复。
The issue with Stream not Open seems to be directly i CFNetwork implementation. (reproducible when using NSConnection and also NSSession). It occurs for requests (responses) that needs to be chunked. A side-effect of this issue is that windows authentication is not working properly and each request needs to go through complete handshaking process. And it is not fixed also in iOS 8.1.
这篇关于“Stream在打开之前发送事件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!