我们经常遇到后台编写的是以 wsdl 的接口地址,遇到这样的接口我们应该怎么处理的?
首先用浏览器直接打开该地址
从上图中可以看出webservice请求的相关信息,这里我们需要知道targetNamespace(命名空间)
拼接含有参数的soap字符串:
+(NSString *)requestfordata:(NSArray *)data andrequestname :(NSString *)name{
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<%@ xmlns=\"****命名空间*****\" id=\"o0\" c:root=\"1\">",name] dataUsingEncoding:NSUTF8StringEncoding]];
for(int i =0;i<data.count;i++)
{
[postBody appendData:[[NSString stringWithFormat:@"%@%@%@",[[data objectAtIndex:i]objectAtIndex:0],[[data objectAtIndex:i]objectAtIndex:1],[[data objectAtIndex:i]objectAtIndex:2]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBody appendData:[[NSString stringWithFormat:@"</%@>", name] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * xmlStr=[[NSString alloc]initWithData:postBody encoding:NSUTF8StringEncoding];
NSString *str = [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>""%@"
"</v:Body>"
"</v:Envelope>", xmlStr];
return str;
}
调用网络请求:
+ (NSDictionary *)postRequestWithUrl:(NSString *)hostUrl andSoapUrl:(NSString *)url{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 设置请求超时时间
manager.requestSerializer.timeoutInterval = 30;
// 返回NSData
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 设置请求头,也可以不设置
[manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", url.length] forHTTPHeaderField:@"Content-Length"];
// 设置HTTPBody
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
return url;
}];
__block NSDictionary *dict ;
[manager POST:hostUrl parameters:url success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
// 把返回的二进制数据转为字符串
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// 利用正则表达式取出<ns1:out></ns1:out>之间的字符串
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=ns1:out\\>).*(?=</ns1:out)" options:NSRegularExpressionCaseInsensitive error:nil];
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];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"-1", @"code",
@"亲,您当前的网络不给力~", @"message",
nil];
}];
while (!dict) {
[NSThread sleepForTimeInterval:0.5];
}
return dict;
}
现在就能够按照正常的请求方式解析得到数据了.....
网友评论