java HttpClient 訪問webservice並解析返回資料
阿新 • • 發佈:2018-11-22
關於webservice的普及就不多說了,直接進入主題吧。
1.導包
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient HttpClient相關包--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/jaxen/jaxen Xpath解析包--> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
2.訪問webservice
2.1訪問
/** * 發起webservice請求 * @param url * @param soap * @param SOAPAction * @return */ public static String doPostSoap(String url, String soap, String SOAPAction) { //請求體 String retStr = ""; // 建立HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(url); // 設定請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout).build(); httpPost.setConfig(requestConfig); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", SOAPAction); StringEntity data = new StringEntity(soap, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 列印響應內容 retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.err.println("response:" + retStr); } // 釋放資源 closeableHttpClient.close(); } catch (Exception e) { e.printStackTrace(); } return retStr; }
2.2 測試
public static void main(String[] args){ String url = "http://192.168.1.3:58080"; //請求體 String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:pm=\"http://www.getpostman.com/\">" + "<soapenv:Header></soapenv:Header>" + " <soapenv:Body>" + " <getTransRecordTop5 xmlns=\"urn:BankYktWebSrv\">" + " <cardid>12273</cardid>" + " </getTransRecordTop5>" + " </soapenv:Body>" + "</soapenv:Envelope>"; String res = doPostSoap(url,soap,""); System.out.println(res); }
返回資料:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://192.168.1.3:58080/BankYktWebSrv.wsdl" xmlns:ns2="urn:BankYktWebSrv"><SOAP-ENV:Header></SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:getTransRecordTop5Response>
<result>
{ "code":0, "message":"success", "data":[ { "card_id":"12273", "amount":"10.00", "trans_time":"2018-11-20 14:07:28", "status":"1" },{ "card_id":"12273", "amount":"10.00", "trans_time":"2018-11-19 14:55:54", "status":"0" },{ "card_id":"12273", "amount":"10.00", "trans_time":"2018-11-17 23:25:50", "status":"0" },{ "card_id":"12273", "amount":".01", "trans_time":"2018-11-17 16:19:03", "status":"0" },{ "card_id":"12273", "amount":".01", "trans_time":"2018-11-16 20:49:02", "status":"0" } ] }
</result>
</ns2:getTransRecordTop5Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
3.解析webservice返回資訊
3.1解析
/**
* 解析webservice的返回結果
* @param str xml內容
* @param nodeName 需要獲取的正文內容節點名
* @return
*/
public static String getWebservicesBody(String str,String nodeName){
String s = "";
try {
Document doc = DocumentHelper.parseText(str);
DefaultXPath xpath = new DefaultXPath("//"+nodeName);
//下面的wsdl檔案地址需要自己更改,返回資料中一般都有
xpath.setNamespaceURIs(Collections.singletonMap("ns1","http://192.168.1.3:58080/BankYktWebSrv.wsdl"));
List list = xpath.selectNodes(doc);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Element node = (Element) iterator.next();
s = node.getText();
}
}catch (Exception e){
e.printStackTrace();
}
System.err.println("Webservices result:"+s);
return s;
}
3.2測試
個人習慣轉json
public static void main(String[] args){
String url = "http://192.168.1.3:58080";
//請求體
String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:pm=\"http://www.getpostman.com/\">" +
"<soapenv:Header></soapenv:Header>" +
" <soapenv:Body>" +
" <getTransRecordTop5 xmlns=\"urn:BankYktWebSrv\">" +
" <cardid>12273</cardid>" +
" </getTransRecordTop5>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
String res = doPostSoap(url,soap,"");
String formatStr = getWebservicesBody(res,"result");
//System.out.println(formatStr);
JSONObject json = JSONObject.parseObject(formatStr);
System.out.println(json);
}
測試結果圖
error輸出的是獲取result節點的資料。
下面是轉json格式的資料。
json我用的是阿里巴巴那個:com.alibaba.fastjson.JSONObject