2015-06-09 154 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>"