AFNetworking 2.x POST request with xml body

做~自己de王妃 提交于 2019-12-13 01:17:24

问题


I am trying to POST some data to the server with body formatted as XML.

[[HttpClientXML sharedClientWithBaseUrl:self.baseUrl] POST:@"/postXml/" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    BSLog(@"Success");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    BSLog(@"Error: %@",error.localizedDescription);
}];

POST body xml content looks smth. like that:

<action name='job'>jobName</action>

HttpClientXML has this definitions:

self.responseSerializer = [AFXMLParserResponseSerializer serializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];

In version 1.x this was doable with AFXMLRequestOperation. But in version 2.x I can not get the working solution with AFHTTPSessionManager.

Any suggestions?


回答1:


Here is the solution...

request.HTTPBody = [self.body dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"POST";
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


来源:https://stackoverflow.com/questions/22711438/afnetworking-2-x-post-request-with-xml-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!