iOS--对接soap协议接口
最近在对接soap协议接口,分别使用AFN和系统自带方法进行解析。soap协议具体是什么就不阐述了,可以自行百度。
说一下需要注意的地方:
1,iOS与soap协议接口对接,参数要传xml格式的字符串,具体格式看接口说明,务必保持一致;
2,soap1.1和soap1.2使用思路是一样的,但是设置的参数不一样,具体看代码;
3,soap协议的返回值也是xml格式的,需要进行xml解析,获取需要节点下的数据(这里强调一点,可以根据某个节点获取需要的数据,但是也可以不管xml的节点,直接通过根节点获取需要的数据,这个是看后台怎么返回的).
首先看一下要传递的参数格式:说白了就是一个请求体,只不过是xml格式的而已
NSString *soapMessage = [NSString stringWithFormat: @"<?xml version= \"1.0\" encoding=\"utf-8\"?> \ <soap:Envelope xmlns:xsi= \"http://www.w3.org/2001/XMLSchema-instance \" xmlns:xsd= \"http://www.w3.org/2001/XMLSchema \" xmlns:soap = \"http://schemas.xmlsoap.org/soap/envelope/\"> \ <soap:Body> \ <xiaoxi xmlns= \"http://tempuri.org/ \"> \ <typeValue>%@</typeValue> \ </xiaoxi> \ </soap:Body> \ </soap:Envelope>",@"147"];
用系统自带的方法进行解析:
#pragma mark - 用系统自带的 - (void)postWithSoapMessage:(NSString *)message { // url NSURL *url = [NSURL URLWithString:XMHTTPADDRESS]; // request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置POST请求方式 [request setHTTPMethod:@"POST"]; // SOAPAction soap1.1必须设置 soap1.2不设置 [request addValue:@"http://tempuri.org/UserLogin" forHTTPHeaderField:@"SOAPAction"]; /* * Content-Type * soap1.1 text/xml; charset=utf-8 * soap1.2 application/soap+xml; charset=utf-8 */ [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // 请求字节数 [request addValue:[NSString stringWithFormat:@"%zd", message.length] forHTTPHeaderField:@"Content-Length"]; // 设置请求体 utf-8编码 [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; // session NSURLSession *session = [NSURLSession sharedSession]; // task NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 结果 NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; XMLog(@"result = %@",result); }]; [task resume]; }
用AFN进行解析
//POST请求 这里稍微封装了下,便于自己使用,看主要的就可以.... - (void)postWithSoapAction:(NSString *)action parameters:(NSString *)soapMessage success:(Success)success failure:(Failure)failure { afnManager.responseSerializer = [AFHTTPResponseSerializer serializer]; afnManager.requestSerializer = [AFHTTPRequestSerializer serializer]; /* * 一般情况AFN请求参数只能传字典 * 但是可以在这个方法里将请求参数变成字符串形式 */ [afnManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) { return soapMessage; }]; // 请求设置 [afnManager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [afnManager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapMessage.length] forHTTPHeaderField:@"Content-Length"]; [afnManager.requestSerializer setValue:[NSString stringWithFormat:@"http://tempuri.org/%@",action] forHTTPHeaderField:@"SOAPAction"]; // post请求 [afnManager POST:XMHTTPADDRESS parameters:soapMessage progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [SVProgressHUD dismiss]; if (responseObject) { NSString *response = [self getJsonStringWithData:responseObject]; success(response); }else { XMLog(@"%@",@"请求失败"); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }]; }
转自:http://www.cnblogs.com/zhangshan/p/5589610.html