搭建環境
開發工具:IntelliJ IDEA 2019.2.4 x64
JDK:1.8.0_202
Tomcat:apache-tomcat-8.5.69
webservice服務端
一、文件-NEW-新建項目
二、
要生成wsdl文件(有的版本可以直接java文件右鍵),我的在tool里
webservice客戶端
使用http方式進行調用
使用Maven的pom.xml增加以下依賴
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
public void GetHttpWebService(String url, String strParams) { String InterfaceName="UserTransmit"; String userName = null; String userPass=null; try { userName = AESHelper.encryptAES("admin"); userPass = AESHelper.encryptAES("pass"); } catch (Exception e) { e.printStackTrace(); } String name="aaa"; //soap 參數 String soapXml =""; soapXml += "<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/\" xmlns:otms=\"http://otms.efh.com.cn\">"; soapXml += " <soap:Header>"; soapXml += " <SoapHeader xmlns=\"http://otms.efh.com.cn/\">"; soapXml+= " <name>" + userName + "</name>"; soapXml+= " <password>" + userPass + "</password>"; soapXml+= " </SoapHeader>"; soapXml+= " </soap:Header>"; soapXml+= " <soap:Body>"; soapXml+= " <otms:" + InterfaceName + ">"; // 接口名稱; if (strParams != null && strParams.length() > 0) { soapXml += " <wno>" + strParams + "</wno>"; // 參數加數據 } soapXml += " </otms:" + InterfaceName + ">"; // 接口名稱; soapXml += "</soap:Body>"; soapXml += "</soap:Envelope>"; //獲取http構建器對象 HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient httpClient = builder.build(); //封裝httppost請求對象 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build()); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", ""); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); //設置post請求參數實體 //執行http請求 CloseableHttpResponse response = httpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印響應內容 String retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(retStr); } // 釋放資源 httpClient.close(); } catch (Exception e) { // logger.error("exception in doPostSoap1_1", e); } }
調用:
String url="http://localhost:8889/PubWebService?wsdl";
GetHttpWebService(url,"helloChenze");
其他

public static void GetHttpWebService(String url, String InterfaceName,String strParams) { String userName = null; String userPass=null; try { userName = AESHelper.encryptAES("admin"); userPass = AESHelper.encryptAES("pass"); } catch (Exception e) { e.printStackTrace(); } String name="aaa"; //soap 參數 String soapXml =""; soapXml += "<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/\" xmlns:otms=\"http://otms.efh.com.cn\">"; soapXml += " <soap:Header>"; soapXml += " <SoapHeader xmlns=\"http://otms.efh.com.cn/\">"; soapXml+= " <name>" + userName + "</name>"; soapXml+= " <password>" + userPass + "</password>"; soapXml+= " </SoapHeader>"; soapXml+= " </soap:Header>"; soapXml+= " <soap:Body>"; soapXml+= " <otms:" + InterfaceName + ">"; // 接口名稱; if (strParams != null && strParams.length() > 0) { soapXml += " <wno>" + strParams + "</wno>"; // 參數加數據 } soapXml += " </otms:" + InterfaceName + ">"; // 接口名稱; soapXml += "</soap:Body>"; soapXml += "</soap:Envelope>"; //獲取http構建器對象 HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient httpClient = builder.build(); //封裝httppost請求對象 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build()); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", ""); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); //設置post請求參數實體 //執行http請求 CloseableHttpResponse response = httpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印響應內容 String retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(retStr); } // 釋放資源 httpClient.close(); } catch (Exception e) { // logger.error("exception in doPostSoap1_1", e); } } GetHttpWebService("http://localhost:8888/PubWebService?wsdl","GetFactoryInfo","{\"componentType\":\"ws\"}");