一、用URLSession请求
NSString *soapStr = [NSString stringWithFormat:@"\
xmlns:soap = \"http://网址\">\ \ // 这中间基本就是要写的参数,在Body 之间就是参数 \
NSURL *url=[NSURL URLWithString:@"你自己wsdl文档中对应的endpoint address"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
// 访问方式[request setHTTPMethod:@"POST"];
// 设置请求头(请求头也可以不设置,前两个设不设置都一样,应该默认的,但是SOAPAction我怎么都设置不对,不设置也可以,干脆不设置了)
// [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];// [request addValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"];
// [request addValue:@"nameSpace/methodName" forHTTPHeaderField:@"SOAPAction"];
// body内容 [request setHTTPBody:[soapStr dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"进入成功回调Session-----结果:%@----请求地址:%@", result, response.URL);
if (error) {
NSLog(@"Session----失败----%@", error.localizedDescription);
}
}];
[task resume];
二、用AFNetworking请求
// 传入一个字典只有唯一(key - value)的数组,
// eg: NSArray *array = @[@{@"addr":@"777"},@{@"999":@"666"},@{@"000":@"111"}];
+ (NSString*)convertDicArrayToXML:(NSArray*)arrayandElement:(NSString*)element {
if(array.count<=0) {
return nil;
}
NSMutableString *mString = [NSMutableString string];
[mStringappendFormat:@"<%@ xmlns=\"http://tempuri.org/\">",element];
for(NSDictionary*dicinarray) {
[mStringappendFormat:@"<%@>", dic.allKeys.firstObject];
[mStringappendFormat:@"%@", dic.allValues.firstObject];
[mStringappendFormat:@"%@>\t\t", dic.allKeys.firstObject];
}
[mStringappendFormat:@"%@>", element];
returnmString;
}
+ (void)xmlRequestPOSTWithSoapBody:(NSString*)bodyStringandElement:(NSString*)elementsuccess:(void(^)(idresponseObject))successfailure:(void(^)(NSError*error))failure {
// DLog(@"....bodyString = %@", bodyString);
NSString*soapStr = [NSString stringWithFormat:
@"\
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 设置请求超时时间
manager.requestSerializer.timeoutInterval = 15;
// 返回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"];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"http://tempuri.org/%@",element] forHTTPHeaderField:@"SOAPAction"];
// 设置HTTPBody
[manager.requestSerializersetQueryStringSerializationWithBlock:^NSString*(NSURLRequest*request,NSDictionary*parameters,NSError*__autoreleasing*error) {
returnsoapStr;
}];
[managerPOST:webService_Urlparameters:soapStrprogress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
//DLog(@"responseObject = %@", responseObject);
// 把返回的二进制数据转为字符串
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// DLog(@"result = %@", result);
// 利用正则表达式取出之间的字符串
NSString*resultString = [NSStringstringWithFormat:@"(?<=%@Result\\>).*(?=%@Result)",element,element];//@"(?<=return\\>).*(?=
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:resultString options:NSRegularExpressionCaseInsensitive error:nil];
NSDictionary *dict = [NSDictionary dictionary]; for(NSTextCheckingResult*checkingResultin[regularmatchesInString:resultoptions:0range:NSMakeRange(0, result.length)]) {
// 得到字典
dict = [NSJSONSerialization JSONObjectWithData:[[result substringWithRange:checkingResult.range] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
}
if(success) {
success(dict);
}
// DLog(@"dic = %@", dict);
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
DLog(@"error = %@", error);
if(failure) {
failure(error);
}
}];
}
调用一下:
+ (void)mallListWithType:(NSString *_Nullable)type
success:(void(^_Nullable)(id_NullableresponseObject))success
failure:(void(^_Nullable)(NSString*_NullablestrError))failure {
NSString *element = @"ZHWS_GetActivityMallAdList";
NSArray*bodyArray =@[
@{@"Type":type}
];
NSString*bodyString = [selfconvertDicArrayToXML:bodyArrayandElement:element];
[selfxmlRequestPOSTWithSoapBody:bodyStringandElement:elementsuccess:^(idresponseObject) {
DLog(@"... 活动相关、商场广告、广告列表 = %@", responseObject);
if(responseObject) {
success(responseObject);
}
}failure:^(NSError*error) {
if(error) {
//failure(error.mj_JSONString);
}
}];
}
近期项目刚好用到这个,之前也没有用过,所以顺手就记录一下。如有错误,看到的小伙伴请指正。
参考链接:https://blog.csdn.net/djh996064549/article/details/79635551