Swift中的SOAP POST从NSURLConnection转移到NSURLSession



我正试图从NSURLConnection转移到NSURLSession以获取SOAP帖子,但NSURLSessionDataDelegate似乎有问题。

以下是NSURLConnection中运行良好的旧代码:

let soapMessage = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://tempuri.org/'><SOAP-ENV:Body><ns1:get_Countries/></SOAP-ENV:Body></SOAP-ENV:Envelope>"
    print("Soap Packet is (soapMessage)")
    let urlString = "https://example.com/Service.svc"
    let url = NSURL(string: urlString)
    let theRequest = NSMutableURLRequest(URL: url!)
    let msgLength = String(soapMessage.characters.count)
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.addValue("http://tempuri.org/IService/get_Countries", forHTTPHeaderField: "SoapAction")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    print("Request is (theRequest.allHTTPHeaderFields!)")
    let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: false)
    connection?.start()

然后,此代码使用NSURLConnectionDelegate,工作正常,如下所示:

func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    MutableData.length = 0;
    let httpresponse = response as? NSHTTPURLResponse
    print("status (httpresponse?.statusCode)")
    //print("headers (httpresponse?.allHeaderFields)")
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
    MutableData.appendData(data)
}

func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    NSLog("Error with Soap call: %@", error)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
    let xmlParser = NSXMLParser(data: MutableData)
    xmlParser.delegate = self
    xmlParser.parse()
    xmlParser.shouldResolveExternalEntities = true
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "example.com" {
        NSLog("yep")
        let credential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
    } else {
        NSLog("nope")
        challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
    }
}

所以这些代码都很好用,仅供参考,这样你就可以看到我过去做了什么,以及API确实有效的事实!但是,如果我转而使用NSURLSession和NSURLSessionDataDelegate,那么我就无法使其正常工作。

这是新代码:

let soapMessage = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://tempuri.org/'><SOAP-ENV:Body><ns1:get_Countries/></SOAP-ENV:Body></SOAP-ENV:Envelope>"
    print("Soap Packet is (soapMessage)")
    let urlString = "https://example.com/Service.svc"
    let url = NSURL(string: urlString)
    let theRequest = NSMutableURLRequest(URL: url!)
    let msgLength = String(soapMessage.characters.count)
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.addValue("http://tempuri.org/IService/get_Countries", forHTTPHeaderField: "SoapAction")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    print("Request is (theRequest.allHTTPHeaderFields!)")
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration:config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let task = session.dataTaskWithRequest(theRequest)
task.resume()

我使用的代理是NSURLSessionDelegate、NSURLSessionDataDelegate:

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
    print("Am in NSURLSessionDelegate didReceiveChallenge")
    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "example.com" {
        NSLog("yep authorised")
        let credential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
    } else {
        NSLog("nope")
        challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
    }
}
func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
    print("Am in URLSessionDidFinishEventsForBackgroundURLSession")
    let xmlParser = NSXMLParser(data: MutableData)
    xmlParser.delegate = self
    xmlParser.parse()
    xmlParser.shouldResolveExternalEntities = true
}
func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
    print("error of (error)")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
    print("Am in didReceiveResponse")
    MutableData.length = 0
}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
    print("Am in didReceiveData")
    MutableData.appendData(data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
    print("error of (error)")
}

所以,当我运行代码时,我会得到输出:

"我在NSURLSessionDelegate didReceiveChallenge"是授权的">

因此,它进入了didReceiveChallenge罚款,并且似乎正在授权HTTPS安全证书罚款,但随后没有发生任何进一步的事情,它没有做任何其他事情,我希望它进入didReceReceiveResponse,然后进入didRReceiveData,但根本没有发生任何其他事情。

所以我被卡住了,我当然可以继续使用NSURLConnection,因为它一切都很好,但我想了解NSURLSession,尤其是我哪里出了问题。所以,如果有人能帮忙,那就太好了。

感谢

如果其他人也有同样的问题,我会解决这个问题。问题是我没有在didReceiveChallenge中使用completionHandler,并且didReceReceiveResponse委派

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {

completionHandler(NSURLSessionResponseDisposition.Allow(//.Cancel,如果要停止下载}

最新更新