Java使用Soap方式調(diào)用WebService接口代碼示例
更新時間:2024年03月02日 14:34:27 作者:xjz_2002
Java調(diào)用WebService接口是指通過Java語言來訪問并與WebService進行交互,WebService是一種基于Web的服務(wù)架構(gòu),它通過標(biāo)準的XML和HTTP協(xié)議來提供服務(wù),這篇文章主要給大家介紹了關(guān)于Java使用Soap方式調(diào)用WebService接口的相關(guān)資料,需要的朋友可以參考下
pom文件依賴
<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.15</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies>
測試類WebServiceTest.java
import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.text.StringEscapeUtils; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.nio.charset.Charset; public class WebServiceTest { public static void main(String[] args) throws JsonProcessingException { String url = "http://192.168.2.243:9018/sjz_mete_api.asmx?op=USMI_SanWeiSecondData"; // 根據(jù)實際情況拼接xml String xmlData = "<soap:Envelope\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" + " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + " <soap:Body>\n" + " <USMI_SanWeiSecondData xmlns=\"http://10.48.98.122:82/\">\n" + " <resultType>" + "json" + "</resultType>\n" + " <arrStation>\n" + " <string>" + "HJ001" + "</string>\n" + " </arrStation>\n" + " <beginDate>" + "2023-12-02 12:00:00" + "</beginDate>\n" + " <endDate>" + "2022-12-02 12:00:59" + "</endDate>\n" + " </USMI_SanWeiSecondData>\n" + " </soap:Body>\n" + "</soap:Envelope>"; String postSoap = doPostSoap(url, xmlData, "http://10.48.98.122:82/USMI_SanWeiSecondData"); JSONObject jsonObject = SoapResponseParser(postSoap); System.out.println("unPostSoap===========" + postSoap); System.out.println("jsonObject===========" + jsonObject); } //soap響應(yīng)的數(shù)據(jù)解析,放到j(luò)son對象中并返回 public static JSONObject SoapResponseParser(String soapResponse) throws JsonProcessingException { // 去除XML轉(zhuǎn)義字符 String jsonContent = StringEscapeUtils.unescapeXml(soapResponse); // 找到JSON數(shù)組的開始位置和結(jié)束位置 int startIndex = jsonContent.indexOf("["); int endIndex = jsonContent.indexOf("]"); JSONObject jsonObject = new JSONObject(); if ((startIndex) != -1 && (endIndex) != -1) { // 提取JSON數(shù)組部分 String jsonString = jsonContent.substring(startIndex, endIndex + 1); // 初始化ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); // 將JSON字符串解析為JsonNode(樹狀結(jié)構(gòu)表示) JsonNode jsonNode = objectMapper.readTree(jsonString); // 如果是JSON數(shù)組,可以直接獲取數(shù)組元素 if (jsonNode.isArray()) { for (JsonNode element : jsonNode) { jsonObject.put("站號", element.get("站號").asText()); jsonObject.put("站名", element.get("站名").asText()); jsonObject.put("日期", element.get("日期").asText()); jsonObject.put("總風(fēng)速", element.get("總風(fēng)速").asText()); jsonObject.put("水平風(fēng)速", element.get("水平風(fēng)速").asText()); jsonObject.put("垂直風(fēng)向", element.get("垂直風(fēng)向").asText()); jsonObject.put("水平風(fēng)向", element.get("水平風(fēng)向").asText()); jsonObject.put("風(fēng)速U", element.get("風(fēng)速U").asText()); jsonObject.put("風(fēng)速V", element.get("風(fēng)速V").asText()); jsonObject.put("風(fēng)速W", element.get("風(fēng)速W").asText()); } } } return jsonObject; } //使用SOAP1.1發(fā)送消息 public static String doPostSoap(String postUrl, String soapXml, String soapAction) { String retStr = ""; // 創(chuàng)建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(postUrl); // 設(shè)置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000) .setConnectTimeout(6000).build(); httpPost.setConfig(requestConfig); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", soapAction); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印響應(yīng)內(nèi)容 retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println("response:" + retStr); } // 釋放資源 closeableHttpClient.close(); } catch (Exception e) { e.printStackTrace(); } return retStr; } }
總結(jié)
到此這篇關(guān)于Java使用Soap方式調(diào)用WebService接口的文章就介紹到這了,更多相關(guān)Java調(diào)用WebService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot源碼分析之bootstrap.properties文件加載的原理
本文通過訪問看到bootstrap.properties中的信息獲取到了,同時age也被application.properties中的屬性覆蓋掉了。加載順序到底是什么?為什么會覆蓋呢?我們接下來分析下吧2021-12-12Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作
這篇文章主要介紹了Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08JAVA利用順序表實現(xiàn)“楊輝三角”的思路及代碼示例
楊輝三角形是中國古代數(shù)學(xué)的杰出研究成果之一,是我國北宋數(shù)學(xué)家賈憲于1050年首先發(fā)現(xiàn)并使用的,這篇文章主要介紹了JAVA利用順序表實現(xiàn)楊輝三角的思路及代碼示例,需要的朋友可以參考下2025-01-01