HTTP直接请求webService
在实际开发中,会遇到各种各样的webService接口,并且对方提供的接口并不规范,一些客户端反而就不好使了,如cxf(客户端与动态调用)等,
直接用java提供的api比较繁琐,这时直接用http request请求更便捷。
可以分为两步,request一个soap消息(xml),然后response一个soap消息(xml),具体如下
/**
* <br>描 述:发送并返回报文消息
* <br>作 者:xieyj
* <br>历 史: (版本) 作者 时间 注释
* @param urlStr wsdl
* @param paraXml 请求的soap消息串,可以用soapui查看
* @param intfMethod 请求的接口方法
* @return
*/
public String sendAndGetResponseData(String urlStr, String paraXml, String intfMethod) {
String respData = "";
try {
URL url = new URL(urlStr);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
//con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
//con.setRequestProperty("WSS-Password Type", "PasswordText");
con.setRequestProperty("SOAPAction", intfMethod);
con.setRequestProperty("Encoding", "UTF-8");
OutputStream reqStream = con.getOutputStream();
reqStream.write(paraXml.getBytes());
//接收报文
InputStream resStream = con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(resStream, "utf-8"));
StringBuffer data = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
data.append(line);
}
//将结果解密
respData= data.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return respData;
}
soap请求串实例,如下
/**
* <br>描 述:请求消息模板
* <br>作 者:xieyj
* <br>历 史: (版本) 作者 时间 注释
* @return
*/
public String getXmlTemplate() {
/**参数描述
* @param systemId 为数据请求系统ID
* @param dataType 传部门ID则出该部门数据,传空出全部数据
* @param bgnDt 增量数据产生开始日期时间 日期时间格式为:YYYY-MM-DD hh24:mi:ss
* @param endDt 增量数据产生结束日期时间 日期时间格式为:YYYY-MM-DD hh24:mi:ss
* @param pageNum 页码,表示请求第几页的数据
* @param pageSize 数据行数,表示请求单页传输的数据,默认为500
* @param userName 用户名,采用DES加密
* @param passWord 用户密码,采用DES加密
*/
StringBuffer template = new StringBuffer();
template.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://com.hps.webservice/admin/services/\">");
template.append("<soapenv:Header/>");
template.append("<soapenv:Body>");
template.append("<ser:${soapAction}>");
template.append("<ser:in0>");
template.append("<![CDATA[<?xml version='1.0' encoding='UTF-8'?><DATA>");
template.append("<parameter name='SYSTEM_ID'>${systemId}</parameter>");
template.append("<parameter name='DATA_TYPE'>${dataType}</parameter>");
template.append("<parameter name='BGN_DT'>${bgnDt}</parameter>");
template.append("<parameter name='END_DT'>${endDt}</parameter>");
template.append("<parameter name='PAGE_NUM'>${pageNum}</parameter>");
template.append("<parameter name='PAGE_SIZE'>${pageSize}</parameter>");
template.append("<parameter name='USERNAME'>${userName}</parameter>");
template.append("<parameter name='PASSWORD'>${passWord}</parameter>");
template.append("</DATA>]]>");
template.append("</ser:in0>");
template.append("</ser:${soapAction}>");
template.append("</soapenv:Body>");
template.append("</soapenv:Envelope>");
return template.toString();
}
占位符替换工具类见 Java占位符替换工具类
浙公网安备 33010602011771号