带有Alamofire的Swift

带有Alamofire的Swift

本文介绍了带有Alamofire的Swift 2.0 Soap请求发送xml参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对此Web服务示例进行请求:



感谢您的帮助。

解决方案

我编写了一个pod,用于将XML映射到对象,名为 。 (使用与相同的技术)



您可以使用子规范轻松创建SOAP请求。 p>

首先创建自己的自定义 SOAPMessage

 类HolidayServiceMessage:SOAPMessage {

var countryCode:字符串?

覆盖功能映射(map:XMLMap){
super.mapping(map:map)

countryCode<-map [ m:countrycode]
}
}

然后创建SOAPEnvelope,例如:

  let soapMessage = HolidayServiceMessage(soapAction: GetHolidaysAvaible,nameSpace: http://holidaywebservice.com/HolidayService_v2)
soapMessage.countryCode =美国

let soapEnvelope = SOAPEnvelope(soapMessage:soapMessage)

最后使用Alamofire发送SOAP请求:

  Alamofire.request( http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx ?wsdl,方法:.post,参数:soapEnvelope.toXML(),编码:XMLEncoding.soap(withAction: http://holidaywebservice.com/HolidayService_v2#GetHolidaysAvaible))

您可以使用 XMLMappable 协议将XML响应映射到快速对象。


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.

解决方案

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.

这篇关于带有Alamofire的Swift 2.0 Soap请求发送xml参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:16