如何用IOS調(diào)用WebService(SOAP接口)
在一次項(xiàng)目開發(fā)過程中,用到IOS調(diào)用WebService接口,所以抽個(gè)空把這方面的內(nèi)容給大家整理出來,分享給大家。
方法一:使用WSDL2ObjC工具,將接口轉(zhuǎn)成OC類。
1.在瀏覽器中輸入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx, 地址后面添加上.wsdl成http://xxx.xxx.asmx.wsdl打開。
2.將頁面另存為wsdl文件,保存的時(shí)候后綴加上.wsdl,保存成如xxxxService.asmx.xml.wsdl。
3.使用WSDL2ObjC工具將wsdl文件轉(zhuǎn)成OC類。
4.將生成的OC類的全部文件導(dǎo)入到項(xiàng)目中。然后就可以調(diào)用了?,F(xiàn)在沒有寫Demo,我就貼一部分調(diào)用代碼,以后有機(jī)會傳個(gè)Demo上來。
- (NSString *)skHkshListOfpagenow2:(NSInteger)aPagenow pagesize:(NSInteger)aPagesize {
MURPXzshServiceSoapBinding *binding = [[MURPXzshServiceSoapBinding alloc]initWithAddress:[NSString stringWithFormat:@"%@%@", self.jjjj, XZSH_SERVICE]];//接口地址
binding.logXMLInOut = YES;//可以直接copy
MURPXzshService_HkshList *parm = [[MURPXzshService_HkshList alloc] init];//接口中方法初始化
parm.xxxx = [umcid stringValue];//接口中方法傳參數(shù)
parm.pagenow = [NSNumber numberWithInteger:aPagenow];//接口中方法傳參數(shù)
parm.pagesize = [NSNumber numberWithInteger:aPagesize];//接口中方法傳參數(shù)
MURPXzshServiceSoapBindingResponse *resp = [binding HkshListUsingParameters:parm];//調(diào)用方法
//下面是返回值
for (id mine in resp.bodyParts) {
if ([mine isKindOfClass:[MURPXzshService_HkshListResponse class]]) {
NSString *resultStr = [mine HkshListResult];
return resultStr;
}
}
return nil;
}
注意:上面我的webService名字是MURPXzshService,Soap是MURPXzshServiceSoapBinding,調(diào)用的方法是HkshList。
方法二:直接通過網(wǎng)絡(luò)請求調(diào)用,解析XML的方法。
不說了,先上代碼:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//假如在這開始請求
NSString *webServiceBodyStr = [NSString stringWithFormat:
@"<Jsjy_yjy xmlns=\"http://murpcn.com/murpwebservice/\">"
"<xxx>34192</xxx>"
"<pagenow>1</pagenow>"
"<pagesize>20</pagesize>"
"</Jsjy_yjy>"];//這里是參數(shù)
NSString *webServiceStr = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"%@\n"
"</soap:Body>\n"
"</soap:Envelope>",
webServiceBodyStr];//webService頭
NSString *SOAPActionStr = [NSString stringWithFormat:@"http://xxx.com/murpwebservice/%@", @"Jsjy_yjy"];//SOAPAction
NSURL *url = [NSURL URLWithString:@"http://xxxx/key/MurpjsjyService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%ld", webServiceStr.length];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
[theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConn) {
NSLog(@"8888哈哈哈笑");
}else {
NSLog(@"5555你給我哭");
}
}
//接收到數(shù)據(jù)的代理
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);//得到想要的XML字符串然后解析
//系統(tǒng)自帶的
NSXMLParser *par = [[NSXMLParser alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[par setDelegate:self];//設(shè)置NSXMLParser對象的解析方法代理
[par parse];//調(diào)用代理解析NSXMLParser對象,看解析是否成功
}
//解析XML
#pragma mark xmlparser
//step 1 :準(zhǔn)備解析
- (void)parserDidStartDocument:(NSXMLParser *)parser{
}
//step 2:準(zhǔn)備解析節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"%@", NSStringFromSelector(_cmd) );
}
//step 3:獲取首尾節(jié)點(diǎn)間內(nèi)容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"%@", string);
}
//step 4 :解析完當(dāng)前節(jié)點(diǎn)
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"%@",NSStringFromSelector(_cmd) );
}
//step 5;解析結(jié)束
- (void)parserDidEndDocument:(NSXMLParser *)parser{
}
//獲取cdata塊數(shù)據(jù)
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
}
以上內(nèi)容就是用IOS調(diào)用WebService(SOAP接口)的方式,希望對大家有所啟迪。
相關(guān)文章
iOS實(shí)現(xiàn)對不同分辨率設(shè)備的字號大小適配方法
下面小編就為大家分享一篇iOS實(shí)現(xiàn)對不同分辨率設(shè)備的字號大小適配方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS實(shí)現(xiàn)轉(zhuǎn)場動畫的3種方法示例
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)轉(zhuǎn)場動畫的3種方法,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
淺談iOS中幾個(gè)常用協(xié)議 NSCopying/NSMutableCopying
下面小編就為大家分享一篇淺談iOS中幾個(gè)常用協(xié)議 NSCopying/NSMutableCopying,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
iOS?UITextView?實(shí)現(xiàn)類似微博的話題、提及用戶效果
這篇文章主要介紹了iOS?UITextView?實(shí)現(xiàn)類似微博的話題、提及功能,基本思路是使用正則匹配出成對的#,再利用UITextView的富文本實(shí)現(xiàn)高亮效果,需要的朋友可以參考下2022-06-06

