问题
I want to do a request to this web service example: http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl
I need to send one parameter "countryCode". I don't know how can i do that with alamofire. And how i get the response to parse the xml result.
This is how i did in postman, but i want to know how do the same in swift.
Thanks for your help.
回答1:
Try it
- Will make a POST request
- URL = http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl
- The Parameters are sent to the encoding section
- encoding: .Custom =>
- Created a mutbleRequest
- a. You can take directly URL or b. New URL
- the important is mutableRequest.HTTBody, it is where the soap-message is placed with the necessary values (type String)
the Headers can vary from according to the web service and API, in one case can have login / password / token or only authorisation
the line --> "Content-Type" : or "Content-Length" depends on the situation.
SWXMLHash.parse(response.data!) is to handle the response, your can see more in [AlamoFire / Usage / Response Handling ] (https://github.com/Alamofire/Alamofire)
Alamofire.request(.POST, "http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl", parameters: nil, encoding: .Custom({ (convertible, params) in //a. let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest // or //b. mutableRequest.URL =NSURL(string: theUrlString) let mutableReques02 = NSMutableURLRequest(URL: theURL!) // //mutableReques02.HTTPBody =.... mutableRequest.HTTPBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:sap-com:document:sap:rfc:functions\"><SOAP-ENV:Body> <hs:GetHolidaysAvaible> <hs:countrycode> UnitedStates</hs:countrycode> </hs:GetHolidaysAvaible> </SOAP-ENV:Body></SOAP-ENV:Envelope>".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) return (mutableRequest, nil)}), headers: ["Username": "login", // Login API "Password": "password" , // Password API "AuthenticatedToken" : "35432", // Authenticated API "Authorization": "Basic nasiouynvo8eyt829409", // Authenticated API "Accept" : "text/xml", "Content-Type" : "text/xml; charset=UTF-8", // Type content and charset accept "Accept-Charset" : "UTF-8",]) .responsePropertyList { response in let xml = SWXMLHash.parse(response.data!) print(xml) }
Note : My English is not very good
回答2:
I wrote a pod for mapping XML to objects, called XMLMapper. (uses the same technique as the ObjectMapper)
You can use the Requests subspec to create easily SOAP requests.
First create your own custom SOAPMessage
:
class HolidayServiceMessage: SOAPMessage {
var countryCode: String?
override func mapping(map: XMLMap) {
super.mapping(map: map)
countryCode <- map["m:countrycode"]
}
}
Then create the SOAPEnvelope like:
let soapMessage = HolidayServiceMessage(soapAction: "GetHolidaysAvaible", nameSpace: "http://holidaywebservice.com/HolidayService_v2")
soapMessage.countryCode = "UnitedStates"
let soapEnvelope = SOAPEnvelope(soapMessage: soapMessage)
Finaly send the SOAP request using Alamofire:
Alamofire.request("http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl", method: .post, parameters: soapEnvelope.toXML(), encoding: XMLEncoding.soap(withAction: "http://holidaywebservice.com/HolidayService_v2#GetHolidaysAvaible"))
You can map the XML response to swift objects using XMLMappable
protocol.
回答3:
I got this from github:
Alamofire doesn't have any built in SOAP or XML support, so you'd have to write your own. Fortunately it's straightforward to create a custom response serializer, but the XML parsing is up to you.
As for the parameters, take a look at the documentation and see which of the built in encoding match your use case.
回答4:
Take a look at SOAPEngine. It supports Swift and seems to be well maintained.
来源:https://stackoverflow.com/questions/33447337/swift-2-0-soap-request-with-alamofire-send-xml-parameters