使用Alamofire使用XML发送HTTP Post请求

时间:2016-04-06 14:45:29

标签: ios xml swift http-post alamofire

我需要在我的程序中通过HTTP Post请求发送XML。但是Alamofire中的ParameterEncoding枚举不支持XML参数。我尝试编写custom参数编码:

Alamofire.request(.POST, "URL String", parameters: Dictionary(), encoding: .Custom({
        (convertible, params) in
            let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
            mutableRequest.URL = NSURL(string: "URL String")
            mutableRequest.HTTPBody = "XML String".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        return (mutableRequest, nil)
    })).authenticate(user: "student", password: "dj78dfGF")
    .response{ (request, response, data, error) in
                        let xml = SWXMLHash.parse(data!)
                        print(xml)
             }

然而,它不起作用。结果似乎没有发送XML。 网址是:

http://openapi.aurin.org.au/csw?service=CSW&request=GetRecords&typeNames=csw:Record

,XML是:

<?xml version="1.0" encoding="UTF-8"?>
<csw:GetRecords
    service="CSW"
    version="2.0.2"
    resultType="results"
    outputSchema="http://www.opengis.net/cat/csw/2.0.2"
    xmlns:ogc="http://www.opengis.net/ogc"
    xmlns:csw="http://www.opengis.net/cat/csw/2.0.2"
    xmlns:gml="http://www.opengis.net/gml">
    <csw:Query typeNames="csw:Record">
        <csw:ElementSetName>full</csw:ElementSetName>
        <csw:Constraint version="1.1.0">
            <ogc:Filter>
                <ogc:PropertyIsLike matchCase="false" wildCard="%" singleChar="_" escapeChar="\">
                    <ogc:PropertyName>dc:subject</ogc:PropertyName>
                    <ogc:Literal>Employment</ogc:Literal>
                </ogc:PropertyIsLike>
            </ogc:Filter>
        </csw:Constraint>
    </csw:Query>
</csw:GetRecords>

有谁知道如何通过HTTP发送XML?非常感谢!

0 个答案:

没有答案