“stream正在打开之前发送一个事件”

我试图用Swift做一个SOAP请求。 从9/9/14运行最新的Xcode / iOS。 我使用NSMutableURLRequest来添加一个HTTPBody到请求信息。 但是,一旦我开始与请求的NSURLConnection ,我得到一个错误“streamxxxxxxxxx正在发送一个事件之前被打开”。 我没有使用任何networking库,只是一个普通的旧的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) } 

Stream not Open的问题似乎是CFNetwork的实现。 (使用NSConnection和NSSession时可重现)。 它发生在需要被分块的请求(响应)上。 这个问题的副作用是Windows身份validation不正常,每个请求都需要经过完整的握手过程。 而且在iOS 8.1中也不是固定的。

我到处search这个问题,最终我传递了错误的凭据在didReceiveAuthenticationChallenge 🙁

检查你是否意外地也这样做

如果这不起作用,请尝试以下操作:

将您的凭据embedded到urlstring中

 NSString *urlString = @"http://username:password@domain.com/home"; [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];