只想调用远处webservice,不构建自己的,看了一些资料都是先构建,再请求。
个人理解webservice提供一种服务,暴露某个地址,调用也是http请求,客户端只要传递正确参数到服务端
能够解析调用的那个累的那个方法,获取参数后将响应信息组装给客户端,来回处理都是xml。因此,未借助第三方jar,只用jdk提供jar做了个请求webservice的例子

Java代码 复制代码 收藏代码
  1. import java.io.BufferedReader;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStream;
  5. import java.io.OutputStreamWriter;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8.  
  9. public class InvokeWS {
  10. public static void main(String[] args) {
  11. try {
  12. //以请求天气service为例
  13. String point ="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
  14. //初始化请求传送的soap信息 soap格式从上面网站可以查到
  15. String soap = getSoapBody("济南");
  16. //获取 建立至webservice节点的连接
  17. URL url = new URL(point);
  18. URLConnection con = url.openConnection();
  19. con.setUseCaches(false);
  20. con.setDoInput(true);
  21. con.setDoOutput(true);
  22. //设置请求header信息
  23. con.setRequestProperty("Content-Type", "text/xml; charset=gbk");
  24. con.setRequestProperty("Content-Length",String.valueOf(soap.length()));
  25. con.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
  26. //发送请求内容 soap至服务端
  27. OutputStream out = con.getOutputStream();
  28. OutputStreamWriter writer = new OutputStreamWriter(out,"gbk");
  29. writer.write(soap);
  30. writer.flush();
  31. writer.close();
  32. //获取响应信息
  33. InputStream in = con.getInputStream();
  34. InputStreamReader reader = new InputStreamReader(in,"utf-8");
  35. BufferedReader br = new BufferedReader(reader);
  36. String str = br.readLine();
  37. while(str !=null){
  38. System.out.println(str);
  39. str = br.readLine();
  40. }
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. private static String getSoapBody(String name){
  47. StringBuffer sb = new StringBuffer();
  48. sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  49. sb.append("<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/\">");
  50. sb.append("<soap:Body>");
  51. sb.append("<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">");
  52. sb.append("<theCityName>").append(name).append("</theCityName>");
  53. sb.append("</getWeatherbyCityName>");
  54. sb.append("</soap:Body>");
  55. sb.append("</soap:Envelope>");
  56. return sb.toString();
  57. }
  58. }

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class InvokeWS {
	public static void main(String[] args) {
		try {
			//以请求天气service为例
			String point ="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
			//初始化请求传送的soap信息   soap格式从上面网站可以查到
			String soap = getSoapBody("济南");
			//获取 建立至webservice节点的连接
			URL url = new URL(point);
			URLConnection con = url.openConnection();
			con.setUseCaches(false);
			con.setDoInput(true);
			con.setDoOutput(true);
			//设置请求header信息 
			con.setRequestProperty("Content-Type", "text/xml; charset=gbk");
			con.setRequestProperty("Content-Length",String.valueOf(soap.length()));
			con.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
			//发送请求内容 soap至服务端
			OutputStream out = con.getOutputStream();
			OutputStreamWriter writer = new OutputStreamWriter(out,"gbk");
			writer.write(soap);
			writer.flush();
			writer.close();
			//获取响应信息 
			InputStream in = con.getInputStream();
			InputStreamReader reader = new InputStreamReader(in,"utf-8");
			BufferedReader br = new BufferedReader(reader);
			String str = br.readLine();
			while(str !=null){
				System.out.println(str);
				str = br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String getSoapBody(String name){
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<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/\">");
		sb.append("<soap:Body>");
		sb.append("<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">");
		sb.append("<theCityName>").append(name).append("</theCityName>");
		sb.append("</getWeatherbyCityName>");
		sb.append("</soap:Body>");
		sb.append("</soap:Envelope>");
		return sb.toString();
	}
}