快速的Web服务通信

时间:2015-06-09 19:43:17

标签: ios web-services swift soap

我正在尝试构建我的第一个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添加到目标命名空间

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

肥皂信息出现问题,经过一些试验和错误后,我发现了正确的信息。

&#13;
&#13;
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>"
&#13;
&#13;
&#13;