如何用IOS调用WebService
2017-04-01 · 知道合伙人软件行家
方法一:使用WSDL2ObjC工具,将接口转成OC类。
在浏览器中输入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx, 地址后面添加上.wsdl成http://xxx.xxx.asmx.wsdl打开。
.将页面另存为wsdl文件,保存的时候后缀加上.wsdl,保存成如xxxxService.asmx.xml.wsdl。
3.使用WSDL2ObjC工具将wsdl文件转成OC类。
4.将生成的OC类的全部文件导入到项目中。然后就可以调用了。现在没有写Demo,我就贴一部分调用代码,以后有机会传个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];//接口中方法传参数
parm.pagenow
= [NSNumber numberWithInteger:aPagenow];//接口中方法传参数
parm.pagesize
= [NSNumber numberWithInteger:aPagesize];//接口中方法传参数
MURPXzshServiceSoapBindingResponse
*resp = [binding HkshListUsingParameters:parm];//调用方法
//下面是返回值
for
(id mine in resp.bodyParts) {
if
([mine isKindOfClass:[MURPXzshService_HkshListResponse class]])
{
NSString
*resultStr = [mine HkshListResult];
return
resultStr;
}
}
return
nil;
}
注意:上面我的webService名字是MURPXzshService,Soap是MURPXzshServiceSoapBinding,调用的方法是HkshList。
方法二:直接通过网络请求调用,解析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>"];//这里是参数
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你给我哭");
}
}
//接收到数据的代理
-
(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字符串然后解析
//系统自带的
NSXMLParser
*par = [[NSXMLParser alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[par
setDelegate:self];//设置NSXMLParser对象的解析方法代理
[par
parse];//调用代理解析NSXMLParser对象,看解析是否成功
}
//解析XML
#pragma
mark xmlparser
//step
1 :准备解析
-
(void)parserDidStartDocument:(NSXMLParser
*)parser{
}
//step
2:准备解析节点
-
(void)parser:(NSXMLParser
*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"%@",
NSStringFromSelector(_cmd) );
}
//step
3:获取首尾节点间内容
-
(void)parser:(NSXMLParser
*)parser foundCharacters:(NSString *)string{
NSLog(@"%@",
string);
}
//step
4 :解析完当前节点
-
(void)parser:(NSXMLParser
*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"%@",NSStringFromSelector(_cmd)
);
}
//step
5;解析结束
-
(void)parserDidEndDocument:(NSXMLParser
*)parser{
}
//获取cdata块数据
- (void)parser:(NSXMLParser
*)parser foundCDATA:(NSData *)CDATABlock{
}
以上内容就是用IOS调用WebService(SOAP接口)的方式