【问题标题】:swift web service communication快速的网络服务通信
【发布时间】:2015-08-24 19:46:44
【问题描述】:

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

@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 添加到目标命名空间

【问题讨论】:

  • 您是否收到错误代码?旁注:NSURLSession 比旧的 NSURLConnection 更可取。你不需要使用 Alamofire,但你应该使用 NSURLSession。
  • 当我使用在本地机器上运行的 web 服务的 url (localhost:8080/beeSmartWS) 时,我得到一个 http 错误代码 200,但另一个 url(目标命名空间)返回错误代码 500

标签: ios web-services swift soap


【解决方案1】:

soap 消息存在问题,经过反复试验,我找到了正确的消息。

var is_SoapMessage: String = "
<soapenv:Envelope xmlns:soapenv=\ "http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\ "url\">
  <soapenv:Header/>
  <soapenv:Body>
    <ns:userLogin>
      <ns:userID>username</ns:userID>
      <ns:password>password</ns:password>
    </ns:userLogin>
  </soapenv:Body>
</soapenv:Envelope>"

【讨论】:

    【解决方案2】:

    我建议使用 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,并且操作按应有的方式异步执行(但由于闭包非常容易)。

    【讨论】:

    • 我试过这样做..但我不知道如何将 Alamofire 库添加到我已经存在的项目中
    • 有几种方法可以做到这一点。可能最简单的方法就是下载它,然后将 Alamofire.swift 添加到您的项目中(如果您的应用程序必须与 iOS 7 一起使用,这是唯一的方法)。如果你这样做,你将不得不调用上传而不是 Alamofire.upload
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2016-02-07
    相关资源
    最近更新 更多