如何使用 AFNetworking 2.5 发送 XML PUT/POST 请求

How to send XML PUT/POST request using AFNetworking 2.5

无法弄清楚我需要如何指定 requestSerializer 才能以 XML 格式发送请求。这是 swift 示例:

var manager = AFHTTPRequestOperationManager()

manager.responseSerializer = AFXMLParserResponseSerializer()
manager.requestSerializer = AFHTTPRequestSerializer()

var parameters = "<regNumber>123</regNumber><pinNumber>234</pinNumber>"
var encodingParameters = parameters.dataUsingEncoding(NSUTF16StringEncoding)

manager.PUT("serviceurl", parameters: encodingParameters, success: {
   //....
},
        failure: {
            ///.....
        }
)

您可以看到我现在正在使用 AFHTTPRequestSerializer() - 但我不确定这是否正确。试图找到类似于 AFXMLParserResponseSerializer() 但请求的东西。

它与 AFJSONRequestSerializer 配合使用效果很好,如下所示:

var manager = AFHTTPRequestOperationManager()

manager.responseSerializer = AFXMLParserResponseSerializer()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions()) 

var parameters = ["regNumber": 123, "pinNumber": 234]

manager.PUT("serviceurl", parameters: parameters, success: {
       //....
    },
            failure: {
                ///.....
            }
    )

任何人都可以使用 AFNetworking 2.5 展示 XML 请求的工作示例吗?

您可以像这样使用 AFHTTPRequestOperation 而不是 AFHTTPRequestOperationManager:

    let xmlString = "<regNumber>123</regNumber><pinNumber>234</pinNumber>"
    let xmlData = xmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    let request = NSMutableURLRequest(URL: NSURL(string: "serviceurl")!)
    request.HTTPMethod = "POST"
    request.HTTPBody = xmlData

    let operation = AFHTTPRequestOperation(request: request);

    operation.setCompletionBlockWithSuccess({ (AFHTTPRequestOperation operation, AnyObject response) -> Void in
        //Response

    }, failure: { (AFHTTPRequestOperation operation, NSError    error) -> Void in
        println(error.localizedDescription)
    })
    operation.start()