webservice

webservice有关的问题。恰好今天下午有点时间,就写一篇文章大概介绍下。由于类型比较多,这里介绍比较常用的wsdl类型的。基础知识就不再介绍。


当然,访问一个webservice可能方法很多,这里我主要介绍两种。


访问中央电视台节目的。


webservice接口地址是:http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx





wsdl路径是:http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl




一般地方告诉你webservice接口地址,在后面加上?wsdl即为wsdl地址。


一、soap方式。

这里并不是严格意义上的纯soap访问。



加上头,以及请求的部分,代码如下,就不多介绍了。自己对照图片看,很好理解。

  1. String address = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx"
  2.         URL url = new URL(address); 
  3.         HttpURLConnection http = (HttpURLConnection) url.openConnection(); 
  4.         http.setDoOutput(true); 
  5.         http.setDoInput(true); 
  6.         http.setRequestMethod("POST"); 
  7.         http.setUseCaches(false); 
  8.         http.setRequestProperty("Content-Type", "text/xml"); 
  9.         http.connect(); 
  10.  
  11.         DataOutputStream out = new DataOutputStream(http.getOutputStream()); 
  12.         String cityId = "-4"
  13.         String content = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getTVstationDataSet xmlns=\"http://WebXml.com.cn/\"><theAreaID>" 
  14.                 + cityId 
  15.                 + "</theAreaID></getTVstationDataSet></soap12:Body></soap12:Envelope>"
  16.         out.writeBytes(content); 
  17.  
  18.         out.flush(); 
  19.         out.close(); 
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(http 
  21.                 .getInputStream())); 
  22.         String line; 
  23.         StringBuffer buffer = new StringBuffer(); 
  24.         while ((line = reader.readLine()) != null) { 
  25.             buffer.append(line); 
  26.         } 
  27.         reader.close(); 
  28.         http.disconnect(); 
  29.         System.out.println(buffer.toString()); 
String address = "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx";
		URL url = new URL(address);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setUseCaches(false);
		http.setRequestProperty("Content-Type", "text/xml");
		http.connect();

		DataOutputStream out = new DataOutputStream(http.getOutputStream());
		String cityId = "-4";
		String content = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getTVstationDataSet xmlns=\"http://WebXml.com.cn/\"><theAreaID>"
				+ cityId
				+ "</theAreaID></getTVstationDataSet></soap12:Body></soap12:Envelope>";
		out.writeBytes(content);

		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(http
				.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		http.disconnect();
		System.out.println(buffer.toString());

返回结果自己就随便处理了。



二、axis方式访问。

axis方式是生成访问所需的java类,这样比较直观。


准备好wsdl2java,这里不推荐工具,建议下载axis1工具包,里面有wsdl2java类。


  1. public static void main(String[] args) { 
  2.         String[] s = new String[] { 
  3.                 "-u"
  4.                 "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl"
  5.                 "-o", "client", "-S", "false", "-t" }; 
  6.         WSDL2Java.main(s); 
  7.  
  8.     } 
public static void main(String[] args) {
		String[] s = new String[] {
				"-u",
				"http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl",
				"-o", "client", "-S", "false", "-t" };
		WSDL2Java.main(s);

	}
-u是wsdl地址,-o是输出目录,别的自己搜索。

运行后生成如下包





复制根目录cn到src下。


能使用的就是stub结尾的java类,可能不止一个,随便用一个就行了。我一般用12stub。


  1. public static void main(String[] args) throws Exception { 
  2.         ChinaTVprogramWebServiceSoap12Stub tv = new ChinaTVprogramWebServiceSoap12Stub( 
  3.                 new URL( 
  4.                         "http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationDataSet"), 
  5.                 null); 
  6.  
  7.         GetTVstationDataSetResponseGetTVstationDataSetResult ss = tv 
  8.                 .getTVstationDataSet(-1); 
  9.         MessageElement[] ms = ss.get_any(); 
  10.         System.out.println(ms[1]); 
  11.         for (MessageElement s : ms) { 
  12.  
  13.             System.out.println(s.toString()); 
  14.         } 
  15.     } 
public static void main(String[] args) throws Exception {
		ChinaTVprogramWebServiceSoap12Stub tv = new ChinaTVprogramWebServiceSoap12Stub(
				new URL(
						"http://webservice.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationDataSet"),
				null);

		GetTVstationDataSetResponseGetTVstationDataSetResult ss = tv
				.getTVstationDataSet(-1);
		MessageElement[] ms = ss.get_any();
		System.out.println(ms[1]);
		for (MessageElement s : ms) {

			System.out.println(s.toString());
		}
	}

参数里的url地址,是你要访问的节点的url,比如要访问第一个图里的getTVstationDataSet功能,点击他,进入一个页面,复制地址栏的地址就行了。


运行就是你想要的结过了。



以上只是简单的介绍一下,webservice其实很简单。多使用几个就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值