標籤:query 協議 自己 let return task 傳回值 節點 form
最近在對接soap協議介面,分別使用AFN和系統內建方法進行解析。soap協議具體是什麼就不闡述了,可以自行百度。
說一下需要注意的地方:
1,iOS與soap協議介面對接,參數要傳xml格式的字串,具體格式看介面說明,務必保持一致;
2,soap1.1和soap1.2使用思路是一樣的,但是設定的參數不一樣,具體看代碼;
3,soap協議的傳回值也是xml格式的,需要進行xml解析,擷取需要節點下的資料(這裡強調一點,可以根據某個節點擷取需要的資料,但是也可以不管xml的節點,直接通過根節點擷取需要的資料,這個是看後台怎麼返回的).
首先看一下要傳遞的參數格式:說白了就是一個請求體,只不過是xml格式的而已
NSString *soapMessage = [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> <xiaoxi xmlns= \"http://tempuri.org/ \"> \ <typeValue>%@</typeValue> </xiaoxi> </soap:Body> </soap:Envelope>",@"147"];
用系統內建的方法進行解析:
#pragma mark - 用系統內建的- (void)postWithSoapMessage:(NSString *)message { // url NSURL *url = [NSURL URLWithString:XMHTTPADDRESS]; // request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 設定POST請求方式 [request setHTTPMethod:@"POST"]; // SOAPAction soap1.1必須設定 soap1.2不設定 [request addValue:@"http://tempuri.org/UserLogin" forHTTPHeaderField:@"SOAPAction"]; /* * Content-Type * soap1.1 text/xml; charset=utf-8 * soap1.2 application/soap+xml; charset=utf-8 */ [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // 請求位元組數 [request addValue:[NSString stringWithFormat:@"%zd", message.length] forHTTPHeaderField:@"Content-Length"]; // 佈建要求體 utf-8編碼 [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; // session NSURLSession *session = [NSURLSession sharedSession]; // task NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 結果 NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; XMLog(@"result = %@",result); }]; [task resume];}
用AFN進行解析
//POST請求 這裡稍微封裝了下,便於自己使用,看主要的就可以....- (void)postWithSoapAction:(NSString *)action parameters:(NSString *)soapMessage success:(Success)success failure:(Failure)failure { afnManager.responseSerializer = [AFHTTPResponseSerializer serializer]; afnManager.requestSerializer = [AFHTTPRequestSerializer serializer]; /* * 一般情況AFN請求參數只能傳字典 * 但是可以在這個方法裡將請求參數變成字串形式 */ [afnManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) { return soapMessage; }]; // 請求設定 [afnManager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [afnManager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapMessage.length] forHTTPHeaderField:@"Content-Length"]; [afnManager.requestSerializer setValue:[NSString stringWithFormat:@"http://tempuri.org/%@",action] forHTTPHeaderField:@"SOAPAction"]; // post請求 [afnManager POST:XMHTTPADDRESS parameters:soapMessage progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [SVProgressHUD dismiss]; if (responseObject) { NSString *response = [self getJsonStringWithData:responseObject]; success(response); }else { XMLog(@"%@",@"請求失敗"); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }];}
轉自:http://www.cnblogs.com/zhangshan/p/5589610.html
iOS--對接soap協議介面