Java 使用Soap方式调用WebService接口
温馨提示:这篇文章已超过390天没有更新,请注意相关的内容是否还可用!
pom文件依赖
(图片来源网络,侵删)
com.fasterxml.jackson.core
jackson-databind
2.13.0
com.alibaba
fastjson
1.2.83
cn.hutool
hutool-all
5.8.15
org.apache.httpcomponents
httpclient
4.5.2
测试类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";
// 根据实际情况拼接xml
String xmlData = "\n" +
" \n" +
" \n" +
" " + "json" + "\n" +
" \n" +
" " + "HJ001" + "\n" +
" \n" +
" " + "2023-12-02 12:00:00" + "\n" +
" " + "2022-12-02 12:00:59" + "\n" +
" \n" +
" \n" +
"";
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);
// 初始化ObjectMapper
ObjectMapper 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 = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient 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;
}
}
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
