记java调用WebService .Wsdl
1.简单webService调用
首先拿到一个wsdl链接,如http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?WSDL
对于其中方法的调用简单来说是httpPost请求+soap格式,
例如调用其中的getTVstationDataSet这个方法
可以打开其对应的方法说明页面,http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?op=getTVstationDataSet
可以在这里看到该方法的传参说明(传参格式soap1.1与soap1.2,若是找不到方法说明页面,应该是使用soap1.1进行传参)
通过以上,我们就可以使用httpClient发送请求,并获得响应内容
代码如下,
/**
* @Author Chen
* @Description //TODO 使用httpclient请求对webService进行请求
* @Date 2019/11/25
* @Param [mapXml使用map用于传参, soapAction传入方法对应的soapAction, method传入方法名]
* @return java.lang.String
**/
public static String doSoap1_1(Map<String, Object> mapXml,String soapAction,String method) {
String xml = "";
String ret;
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
//这里将wsdl地址填入
HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl");
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout( 3000 )
.setConnectTimeout( 3000 ).build();
httpPost.setConfig(requestConfig);
try {
//这里添加入请求头信息
httpPost.setHeader("Content-Type", "text/xml; charset=utf-8" );
httpPost.setHeader("SOAPAction", soapAction );
//对soap信息进行拼接
xml += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xds=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <" + method + " xmlns=\""+targetNameSpace+"\">\n";
int i = 0;
for ( String key : mapXml.keySet() ) {
// xml += " <arg" + i + "><![CDATA[" + mapXml.get( key ) + "]]></arg" + i + ">\n";
xml += " <" + key + ">" + mapXml.get( key ) + "</" + key + ">\n";
i++;
}
xml += " </" + method + ">\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
StringEntity data = new StringEntity( xml, Charset.forName("UTF-8") );
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
ret= EntityUtils.toString(httpEntity, "UTF-8");
closeableHttpClient.close();
}else{
System.out.println("无返回!请检查参数后重试!");
ret="404";
closeableHttpClient.close();
}
// 释放资源
} catch (Exception e) {
System.out.println("Line96:"+"exception in doPostSoap1_1" + e);
return "404";
}
return ret;
}
public static void main(String [] args){
//当传入多参数时,会有次序问题,注意顺序
Map map=new LinkedHashMap();
map.put("theAreaID",25);
String getTVstationDataSet = doSoap1_1(map, "http://WebXml.com.cn/getTVstationDataSet", "getTVstationDataSet");
System.out.println("返回结果"+getTVstationDataSet);
}
调用main方法,成功拿到返回的soap数据,若请求时,需要连续请求,请在doSoap1-1中的 closeableHttpClient.close() 全部注释掉
2.复杂的WebService调用
当参数较多,或接受值较为复杂时,一方面需要对数据进行构造,另一方面需要对接受值进行解析
此时要使用java去生成客户端
1.使用java jdk原生工具进行生成
2.使用eclipse 的httpClient进行生成
第一种方式可以参见以下链接
简书-wsimport生成接口代码
那接下简单介绍下第二种方法,使用工具eclipse
首先说一下我的eclipse版本
接下来,使用eclipse创建一个project
接下来输入项目名并直接finish(笔者使用的是jdk1.8)
这样在项目目录中得到了test项目
接下来开始使用httpClient生成代码,步骤如下
此时在项目中就生成了客户端代码
新建一个class文件,对方法进行调用,代码如下
package cn.com.WebXml;
import java.rmi.RemoteException;
import org.apache.axis.message.MessageElement;
import sun.applet.Main;
public class test {
public static void main(String[] args) {
//构建客户端
ChinaTVprogramWebServiceSoapProxy proxy = new ChinaTVprogramWebServiceSoapProxy();
GetAreaDataSetResponseGetAreaDataSetResult areaDataSet=null;
try {
//使用“getAreaDataSet方法”
areaDataSet = proxy.getAreaDataSet();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//输出响应数据
MessageElement[] get_any = areaDataSet.get_any();
for(MessageElement element:get_any){
System.out.println("value====="+element);
}
}
}
调用main方法,发现报错警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
缺少依赖包,在工程中导入需要的jar包
运行代码即可,返回内容如下: