只想调用远处webservice,不构建自己的,看了一些资料都是先构建,再请求。
个人理解webservice提供一种服务,暴露某个地址,调用也是http请求,客户端只要传递正确参数到服务端
能够解析调用的那个累的那个方法,获取参数后将响应信息组装给客户端,来回处理都是xml。因此,未借助第三方jar,只用jdk提供jar做了个请求webservice的例子
- 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();
- }
- }
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();
}
}
转载于:https://blog.51cto.com/daheyuan/1134223