2015-06-09 121 views
1

我试图建立我的第一个应用迅速和我有,当我试图消耗SOAP Web服务的麻烦.. 下面是方法:迅速Web服务通信

@IBAction FUNC callWebService(发件人: 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服务的详细信息发送: Web服务类型:Axis2中(Java和基于SOAP) Web服务的WSDL链接:http://87.106.20.80:8080/axis2/services/app2beeService?wsdl目标命名空间:http://bee.myservice.com SOAP动作:添加/ USERLOGIN目标命名空间

+0

你得到一个错误代码?注意:NSURLSession比旧的NSURLConnection更可取。你不需要使用Alamofire,但是你应该使用NSURLSession。 –

+0

当我使用我的本地机器(http:// localhost:8080/beeSmartWS /)上运行的web服务的url但其他url(目标名称空间)返回错误代码500 – pioneerhear

回答

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正在使用的人很多,和操作都是异步执行的,因为他们应该(但很容易由于闭包)。

+0

时,我得到一个http错误代码200我试过了它..但我不知道如何将Alamofire库添加到我现有的项目 – pioneerhear

+0

有几种方法可以做到这一点。可能最简单的就是下载它,然后将Alamofire.swift添加到您的项目中(这是您的应用必须与iOS 7配合使用的唯一方法)。如果你这样做,你将不得不调用上传而不是Alamofire.upload – ncerezo

0

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

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>"