Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

ios - "Stream is sending an event before being opened"

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!

Relevant code in use:

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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...