当前位置: 首页 > news >正文

Java 使用Soap方式调用WebService接口

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";// 根据实际情况拼接xmlString 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响应的数据解析,放到json对象中并返回public static JSONObject SoapResponseParser(String soapResponse) throws JsonProcessingException {// 去除XML转义字符String jsonContent = StringEscapeUtils.unescapeXml(soapResponse);// 找到JSON数组的开始位置和结束位置int startIndex = jsonContent.indexOf("[");int endIndex = jsonContent.indexOf("]");JSONObject jsonObject = new JSONObject();if ((startIndex) != -1 && (endIndex) != -1) {// 提取JSON数组部分String jsonString = jsonContent.substring(startIndex, endIndex + 1);// 初始化ObjectMapperObjectMapper objectMapper = new ObjectMapper();// 将JSON字符串解析为JsonNode(树状结构表示)JsonNode jsonNode = objectMapper.readTree(jsonString);// 如果是JSON数组,可以直接获取数组元素if (jsonNode.isArray()) {for (JsonNode element : jsonNode) {jsonObject.put("站号", element.get("站号").asText());jsonObject.put("站名", element.get("站名").asText());jsonObject.put("日期", element.get("日期").asText());jsonObject.put("总风速", element.get("总风速").asText());jsonObject.put("水平风速", element.get("水平风速").asText());jsonObject.put("垂直风向", element.get("垂直风向").asText());jsonObject.put("水平风向", element.get("水平风向").asText());jsonObject.put("风速U", element.get("风速U").asText());jsonObject.put("风速V", element.get("风速V").asText());jsonObject.put("风速W", element.get("风速W").asText());}}}return jsonObject;}//使用SOAP1.1发送消息public static String doPostSoap(String postUrl, String soapXml, String soapAction) {String retStr = "";// 创建HttpClientBuilderHttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// HttpClientCloseableHttpClient closeableHttpClient = httpClientBuilder.build();HttpPost httpPost = new HttpPost(postUrl);// 设置请求和传输超时时间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) {// 打印响应内容retStr = EntityUtils.toString(httpEntity, "UTF-8");System.out.println("response:" + retStr);}// 释放资源closeableHttpClient.close();} catch (Exception e) {e.printStackTrace();}return retStr;}}
http://www.lryc.cn/news/292533.html

相关文章:

  • 2024美赛数学建模所有题目思路分析
  • Docker容器引擎(5)
  • 百分点科技:《数据科学技术: 文本分析和知识图谱》
  • LabVIEW传感器通用实验平台
  • 向日葵企业“云策略”升级 支持Android 被控策略设置
  • 51单片机通过级联74HC595实现倒计时秒表Protues仿真设计
  • 深信服技术认证“SCCA-C”划重点:深信服云计算关键技术
  • Redis stream特性了解
  • 苍穹外卖项目可以写的简历和如何优化简历
  • C++:智能指针
  • 用户界面(UI)、用户体验(UE)和用户体验(UX)的差异
  • react 之 UseReducer
  • C++:this隐藏参数
  • MySQL事务原理-相关日志
  • 内核Oops的几种定位方法
  • 外包干了10个月,技术退步明显.......
  • 2024美赛C完整思路
  • Backtrader 文档学习- Broker - Cheat-On-Open
  • 基于微信浙江杭州某停车场车位预约小程序系统设计与实现 研究背景和意义、国内外现状
  • 编程流程图
  • 2024年1月29日-2月4日(全面进行+收集虚幻商城免费资源)
  • 【python接口自动化】- 正则用例参数化
  • Java中的四种线程池详解及使用场景
  • Google Chrome 常用的几个参数
  • Keil软件某些汉字输出乱码,0xFD问题,51单片机
  • 自然语言开发AI应用,利用云雀大模型打造自己的专属AI机器人
  • Android中 Gradle与 AGP 版本对应关系表
  • Linux基础知识合集
  • 跟着pink老师前端入门教程-day13
  • go语言基础之泛型