? 最近開發(fā),用到iOS調(diào)用webService接口(接口地址最后是 wsdl:網(wǎng)絡(luò)服務(wù)描述語言是Web Service的描述語言明刷,它包含一系列描述某個web service的定義婴栽。),首先這里要先了解一下什么是webservice,參考百度百科,這里說道webservice,當然離不開XML,當你要解決一個問題的時候首先要弄明白這個問題.
? ?好了廢話不多說了,說一下我解決的思路,這里我們還是通過http請求的方式調(diào)用webservice的接口方法,通過SOAP方式,這里就是一個標準的請求XML結(jié)構(gòu)如下:
將這個XML作為request的body請求.作為返回數(shù)據(jù),一般也是XML的類型,這里作為一個被AFnetworking慣壞了的孩子殺人的心都有了,但是隨著蘋果原生功能越來越完善,老子就信了你的邪,自己寫了一個,調(diào)用傳參參照af的方式,在方法內(nèi)部進行XML的拼接和解析,XML解析這塊用的是谷歌的GDataXML.
這里有個題外話,如果你這里也要上傳圖片,那么需要和后臺確認,1.直接上傳nsdata數(shù)據(jù)2.把照片的data數(shù)據(jù)base64加密上傳字符串?
由于初次使用簡書,代碼在:http://www.reibang.com/p/acff1af35610
```
/**
*請求SOAP,返回NSData
*
*? @param url請求地址
*? @param soapBody soap的XML中方法和參數(shù)段
*? @param success成功block
*? @param failure失敗block
*/
+ (void)SOAPData:(NSString*)url soapBody:(NSDictionary*)bodyParams success:(void(^)(idresponseObject))success failure:(void(^)(NSError*error))failure {
NSString*paramsStr =@"";
for(NSString*keyinbodyParams.allKeys) {
if(![keyisEqualToString:@"method"] ) {
paramsStr = [NSStringstringWithFormat:@"%@<%@>%@",paramsStr,key,bodyParams[key],key];
}
}
//調(diào)用的方法+ (命名空間:這個對應wsdl文檔的命名空間)
NSString*bodyStr = [NSStringstringWithFormat:
@"<%@ xmlns = \"http://tempuri.org/\">%@\n",bodyParams[@"method"],paramsStr,bodyParams[@"method"]];
NSString*soapStr = [NSStringstringWithFormat:
@"\n"
"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
"%@\n"
"\n",bodyStr];
NSMutableURLRequest*request=[NSMutableURLRequestrequestWithURL:[NSURLURLWithString:url]];
NSString*msgLength = [NSStringstringWithFormat:@"%lu", (unsignedlong)[soapStrlength]];
//以下對請求信息添加屬性前四句是必有的辈末,第五句是soap信息愚争。
[requestaddValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
//[request addValue: @"暫不設(shè)置,使用默認值" forHTTPHeaderField:@"SOAPAction"];
[requestaddValue: msgLengthforHTTPHeaderField:@"Content-Length"];
[requestsetHTTPMethod:@"POST”];//因為body可能很長所以選擇POST方式
[requestsetHTTPBody: [soapStrdataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession*session = [NSURLSessionsharedSession];
//線程安全,顯示小菊花作為網(wǎng)絡(luò)的提示狀態(tài)(HUDTool是對MBProgress進行了封裝)
dispatch_main_async_safe(^{
AppDelegate*app = (AppDelegate*)[UIApplicationsharedApplication].delegate;
[HUDToolshowToView:app.window];
});
NSURLSessionDataTask*task = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
dispatch_main_async_safe(^{
AppDelegate*app = (AppDelegate*)[UIApplicationsharedApplication].delegate;
[HUDToolhideForView:app.window];
});
NSString*result = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
if(error) {
LOG(@"Session----失敗----%@", error.localizedDescription);
if(failure) {
dispatch_main_async_safe(^{
failure(error);
});
}
}else{
LOG(@"進入成功回調(diào)Session-----結(jié)果:%@----請求地址:%@", result, response.URL);
NSError*error =nil;
GDataXMLDocument*document = [[GDataXMLDocumentalloc]initWithData:dataerror:&error];
//獲取根節(jié)點及中間所有的節(jié)點GDataXMLElement類表示節(jié)點
//獲取根節(jié)點
GDataXMLElement*rootElement = [documentrootElement];
//追蹤到有效父節(jié)點Result(分析返回結(jié)果的XML與當前調(diào)用方法的規(guī)律)
//1.第一層soap:body解析
GDataXMLElement*soapBody=[[rootElementelementsForName:@"soap:Body"]objectAtIndex:0];
//2.第二層不同方法的Response解析
GDataXMLElement*response=[[soapBodyelementsForName:[NSStringstringWithFormat:@"%@Response",bodyParams[@"method"]]]objectAtIndex:0];
//3.第三層不同方法的Result解析
GDataXMLElement*result=[[responseelementsForName:[NSStringstringWithFormat:@"%@Result",bodyParams[@"method"]]]objectAtIndex:0];
NSMutableDictionary*resultDic = [NSMutableDictionarydictionaryWithCapacity:1];
NSArray*arr = result.children;
if(result.childCount==1) {
[resultDicsetValue:[resultstringValue]forKey:@"result"];
}else{
for(GDataXMLElement*elementinarr) {
NSString*str = [[[resultelementsForName:element.name]objectAtIndex:0]stringValue];
if(str !=nil) {
[resultDicsetValue:strforKey:element.name];
}
}
}
if(success) {
dispatch_main_async_safe(^{
success(resultDic);
});
}
}
}];
[taskresume];
}
```