您的位置:首页 > 移动开发 > IOS开发

iOS WebService SOAP 请求的实现

2017-10-10 10:44 369 查看
SOAP是简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。

由于SOAP是基于XML的我们在传输过程中请求参数和返回内容都是XML格式,在这里我使用了XMLDictionary 转换工具

导入XMLDictionary

使用cocoapods导入 pod 'XMLDictionary'

github连接:https://github.com/nicklockwood/XMLDictionary

AFN实现SOAP

//我使用的该类是继承于AFHTTPSessionManager的
//ps 在soapStr中自己要设置命名空间
/**
Post方式请求

@param methodString 方法名
@param parameter 参数
@param successBlock 成功回调
@param isShowHud 是否显示ProgressHUD
*/
- (void)httpPostRequestWithMethodString:(NSString *)methodString withParameters:(NSDictionary *)parameter success:(SuccessBlock)successBlock failure:(FailureBlock)failureBlock isShowHUD:(BOOL)isShowHud
{

if (isShowHud) {
[MBProgressHUD showMessage:@"正在加载"];
}

NSString *soapStr = [NSString stringWithFormat:@"<v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\"
xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"> <v:Header /> <v:Body
xmlns=\"命名空间这个自己问后台要\" id=\"o0\" c:root=\"1\"> %@ </v:Body> </v:Envelope>",[@{methodString:parameter} XMLString]];

//请求头根据后台需要设置
[self.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//在该处设置请求的SOAP请求内容
[self.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
return soapStr;
}];

[self POST:@"调用地址问后台要" parameters:soapStr progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

[MBProgressHUD hideHUD];

NSString *result = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
//        NSLog(@"result:%@",result);
successBlock([NSDictionary dictionaryWithXMLString:result]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

[MBProgressHUD showError:error.localizedDescription];
failureBlock(error);
}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: