平时调接口都是直接用 AFN 把接口地址放到 post 方法中就可以获取到需要的数据,但如何调用 java或.net 的 webservice 接口,以登录方法为例:
地址是asmx的:
这时候需要通过 AFN 发送 soap 请求。
- 构建 soap 请求体
NSString *soapbody = [[NSString alloc] init];
for(NSString *str in pratams.allKeys)
{
soapbody = [soapbody stringByAppendingString:[NSString stringWithFormat:@"<%@>%@%@>\\",str,[pratams objectForKey:str],str]];
}
2.根据后台soap格式,拼接 soap 字符串(命名空间可能需要修改 )
//使用命名空间: http://tempuri.org/
NSString *soapStr = [NSString stringWithFormat:
@"\
\
\
\
\
<%@ xmlns=\"http://tempuri.org/\">\
%@\
%@>\
\
",soapMethod,soapbody,soapMethod];
- 对 AFN 相关参数进行设置
AFHTTPSessionManager *manager = [ AppDelegate shareManager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 设置请求超时时间
manager.requestSerializer.timeoutInterval = 10;
// 返回NSData
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置请求头,也可以不设置
[manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"];
// 设置HTTPBody
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
return soapStr;
}];
- post 方法
[manager POST:@"地址" parameters:soapStr progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 把返回的二进制数据转为字符串
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// 利用正则表达式取出 之间的字符串
NSString *regularStr = [NSString stringWithFormat:@"(?<=%@Result\\>).*(?=%@Result)",soapMethod,soapMethod];
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:regularStr options:NSRegularExpressionCaseInsensitive error:nil];
NSDictionary *dict = [NSDictionary dictionary];
for (NSTextCheckingResult *checkingResult in [regular matchesInString:result options:0 range:NSMakeRange(0, result.length)]) {
// 得到字典
dict = [NSJSONSerialization JSONObjectWithData:[[result substringWithRange:checkingResult.range] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
}
// 请求成功并且结果有值把结果传出去
if (success && dict) {
success(dict);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
//请求失败
}
}];
完整代码如下:
+ (void)SOAPData:(NSDictionary *)pratams soapMethod:(NSString *)soapMethod success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
NSString *soapbody = [[NSString alloc] init];
for(NSString *str in pratams.allKeys)
{
soapbody = [soapbody stringByAppendingString:[NSString stringWithFormat:@"<%@>%@%@>\\",str,[pratams objectForKey:str],str]];
}
//使用命名空间: http://tempuri.org/
NSString *soapStr = [NSString stringWithFormat:
@"\
\
\
\
\
<%@ xmlns=\"http://tempuri.org/\">\
%@\
%@>\
\
",soapMethod,soapbody,soapMethod];
AFHTTPSessionManager *manager = [ AppDelegate shareManager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 设置请求超时时间
manager.requestSerializer.timeoutInterval = 10;
// 返回NSData
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置请求头,也可以不设置
[manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"];
// 设置HTTPBody
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
return soapStr;
}];
[manager POST:@"地址" parameters:soapStr progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 把返回的二进制数据转为字符串
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// 利用正则表达式取出 之间的字符串
NSString *regularStr = [NSString stringWithFormat:@"(?<=%@Result\\>).*(?=%@Result)",soapMethod,soapMethod];
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:regularStr options:NSRegularExpressionCaseInsensitive error:nil];
NSDictionary *dict = [NSDictionary dictionary];
for (NSTextCheckingResult *checkingResult in [regular matchesInString:result options:0 range:NSMakeRange(0, result.length)]) {
// 得到字典
dict = [NSJSONSerialization JSONObjectWithData:[[result substringWithRange:checkingResult.range] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
}
// 请求成功并且结果有值把结果传出去
if (success && dict) {
success(dict);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
[[FunctionManager sharedHandel] showAlert:@"网络连接失败" duration:1.0];
}
}];
}
调用(比如登录):
NSDictionary * paramDict = @{@"userName":_nameTextField.text,@"passWord":_passwordTextField.text};
[AFNwebManager SOAPData:paramDict soapMethod:@"getUserList" success:^(id responseObject) {
NSLog(@"%@",responseObject);
//获取到数据,进行操作
} failure:^(NSError *error) {
//获取失败
}];
如果地址是wsdl的:
注意 soap 请求体的标签可能会不一样,可以下载并借助 soapUI (下载后将接口网址放上去,会生成对应的 soap 格式)
完整代码:
- (void)SOAPData:(NSDictionary *)pratams soapMethod:(NSString *)soapMethod success:(void (^)(id))success failure:(void (^)(void))failure
{
NSString *soapbody = [[NSString alloc] init];
for(NSString *str in pratams.allKeys)
{
soapbody = [soapbody stringByAppendingString:[NSString stringWithFormat:@"<%@>%@%@>\\",str,[pratams objectForKey:str],str]];
}
////.net http://tempuri.org/
//java 与。net的标签不一样,需要借助soapUI查看
NSString *soapStr = [NSString stringWithFormat:
@"\
\
\
\
\
\
%@\
\
\
",soapMethod,soapbody,soapMethod];
self.manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 设置请求超时时间
self.manager.requestSerializer.timeoutInterval = 10;
// 返回NSData
self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置请求头,也可以不设置
[self.manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[self.manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"];
// self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/xml",@"text/json", @"text/plain", @"text/html", nil];
// 设置HTTPBody
[self.manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
return soapStr;
}];
self.task = [self.manager POST:HTTP_STR1 parameters:soapStr progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 把返回的二进制数据转为字符串
NSString *result = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];
// 利用正则表达式取出 之间的字符串
NSString *regularStr = [NSString stringWithFormat:@"(?<=return\\>).*(?=