ios - 快速网络服务通信

标签 ios web-services swift soap

我正在尝试构建我的第一个 swift 应用程序,但在尝试使用 soap 网络服务时遇到了问题。 方法如下:

@IBAction func callWebService(sender: AnyObject) {

           var userName:NSString = "abc"
           var password:NSString = "def"


            var soapMsg = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:xsi='http://87.106.20.80:8080/axis2/services/app2beeService?wsdl' xmlns:xsd='http://localhost:8080/beeSmartWS' xmlns:soap='http:/bee.myservice.com/userLogin'><soap:Body><userLogin xmlns 'http://87.106.20.80:8080/axis2/services/app2beeService?wsdl/userLogin><:userId>" + userName + "</userId><password>" + password + "</password></userLogin></soap:Body></soap:Envelope>"




                //let soapAction = "http://bee.myservice.com/userLogin"
                //let urlString = "http://bee.myservice.com/"




            let urlString: String = "http://bee.myservice.com/"
            var url: NSURL = NSURL(string: urlString)!
            //var request: NSURLRequest = NSURLRequest(URL: url)
            var request = NSMutableURLRequest(URL: url)
            var msgLength = String(countElements(soapMsg))

                request.HTTPMethod = "POST"
                request.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
                request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
                request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
                request.addValue("http://bee.myservice.com/userLogin", forHTTPHeaderField: "Action")



            var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)!
            connection.start()
            println(request)

            var response: NSURLResponse?

            var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

            if let httpResponse = response as? NSHTTPURLResponse {
                println("error \(httpResponse.statusCode)")
                println(response)
            }

            }

发送此请求时我没有收到任何响应

网络服务详情: Web 服务类型:Axis2(基于 Java 和 SOAP) Web 服务 WSDL 链接:http://87.106.20.80:8080/axis2/services/app2beeService?wsdl目标命名空间:http://bee.myservice.com SOAP 操作:将/userLogin 添加到目标 namespace

最佳答案

我建议使用 Alamofire ( https://github.com/Alamofire/Alamofire )。 您的代码如下所示:

var request = NSMutableURLRequest(URL: NSURL(string: "http://bee.myservice.com"))
let msgLength = String(countElements(soapMsg))
let data = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

request.HTTPMethod = "POST"
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.addValue("http://bee.myservice.com/userLogin", forHTTPHeaderField: "Action")

Alamofire.upload( request, data )
    .response { (request, response, data, error) in
        // do stuff here
    }

Alamofire 被很多人使用,并且操作按照它们应该的方式异步执行(但由于闭包非常容易)。

关于ios - 快速网络服务通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30741411/

相关文章:

mysql - Web API 没有得到正确的结果

ios - iPad 横向模式 - 如何添加两个启动画面

iphone - 使用 Google Toolbox for Mac OAuth for Google App Engine

ios - 为什么在我添加自动布局约束时我的 textview 不显示?

web-services - 请求失败,HTTP 状态为 401 : Unauthorized

ios - Swift Framework 仅在干净之后构建

ios - iPad 横向启动图像逆时针旋转 90 度

web-services - 在没有加密的情况下通过网络向用户发送 SSL-pubkey 是否安全?

swift - 将原生 swift Set 转换为数组

ios - 错误 : unable to read module map contents from 'Target Support Files in iOS