在實際開發中,會遇到各種各樣的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占位符替換工具類
