一庵朝、用URLSession請求
NSString *soapStr = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:xsi = \"htttp://www.網(wǎng)址.com"\
xmlns:soap = \"http://網(wǎng)址\">\
<soap:header>\
</soap:header>\
<soap:Body>\
// 這中間基本就是要寫的參數(shù),在Body 之間就是參數(shù)
</soap:Body>\
</soap:Envelope>"];?
NSURL *url=[NSURL URLWithString:@"你自己wsdl文檔中對應(yīng)的endpoint address"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
// 訪問方式[request setHTTPMethod:@"POST"];
// 設(shè)置請求頭(請求頭也可以不設(shè)置长已,前兩個設(shè)不設(shè)置都一樣乾吻,應(yīng)該默認的,但是SOAPAction我怎么都設(shè)置不對,不設(shè)置也可以快压,干脆不設(shè)置了)
// [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內(nèi)容 [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(@"進入成功回調(diào)Session-----結(jié)果:%@----請求地址:%@", result, response.URL);
if (error) {
NSLog(@"Session----失敗----%@", error.localizedDescription);
}
}];
[task resume];
二、用AFNetworking請求
// 傳入一個字典只有唯一(key - value)的數(shù)組垃瞧,
// 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:
?? ? ? ? ? ? ? ? ? ? ? ? @"<?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>%@</soap:Body>\
</soap:Envelope>",bodyString];
? ? AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
? ? manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
? ? // 設(shè)置請求超時時間
? ? manager.requestSerializer.timeoutInterval = 15;
? ? // 返回NSData
? ? manager.responseSerializer = [AFHTTPResponseSerializer serializer];
? ? // 設(shè)置請求頭蔫劣,也可以不設(shè)置
? ? [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"];
? ? // 設(shè)置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);
? ? ? ? // 把返回的二進制數(shù)據(jù)轉(zhuǎn)為字符串
? ? ? ? NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
?? ? ? // DLog(@"result = %@", result);
? ? ? ? // 利用正則表達式取出之間的字符串
? ? ? ? NSString*resultString = [NSStringstringWithFormat:@"(?<=%@Result\\>).*(?=</%@Result)",element,element];//@"(?<=return\\>).*(?=</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);
? ? ? ? }
? ? }];
}
調(diào)用一下:
+ (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(@"... 活動相關(guān)、商場廣告皆警、廣告列表 = %@", responseObject);
? ? ? ? if(responseObject) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? }failure:^(NSError*error) {
? ? ? ? if(error) {
? ? ? ? ? ? //failure(error.mj_JSONString);
? ? ? ? }
? ? }];
}
近期項目剛好用到這個拦宣,之前也沒有用過,所以順手就記錄一下信姓。如有錯誤鸵隧,看到的小伙伴請指正。
參考鏈接:https://blog.csdn.net/djh996064549/article/details/79635551