SOAP XML发送请求及解析返回的XML
导包
org.apache.httpcomponents httpclient 4.5.13
go
利用看看请求xml的格式
package com.common;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 javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;/*** Soap工具类** @author XianYao* @version V1.0.0* @date 2022/3/8 15:40*/
public class SoapUtil {public static void main(String[] args) {String xmlStr = "" +"991 " +"嘉华园 " +" " +"10 " +"1 " +"";//webService接口地址String postUrl = "http://123.456.789:1234/ResWebservice/services/NewAddrService";//"http://tempuri.org/execute";String soapAction = "";//访问接口String result = SoapUtil.SplicingMessage(xmlStr, postUrl, soapAction, "searchAddr", "ns1:searchAddrResponse", "searchAddrReturn");System.out.println("返回的数据为:" + result);}/*** 访问webservice接口** @param postUrl webService接口地址* @param soapAction soapAction地址* @param method 方法名* @param node 节点* @param childNode 子节点* @return 返回值*/public static String SplicingMessage(String xmlBodyStr, String postUrl, String soapAction, String method, String node, String childNode) {//自定义soap报文模板String xmlStr = " +"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:addr=\"http://addr.webservice.ztesoft.com\">" +" " +" " +" + method + " soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +" " +" + xmlBodyStr + "]]>" +" " +" + method + ">" +" " +" ";System.out.println("自定义soap报文模板 ===> " + xmlStr);return doPostSoap(postUrl, xmlStr, soapAction, node, childNode);}/*** 使用SOAP发送消息** @param postUrl webService接口地址* @param soapXml 请求体* @param soapAction soapAction*/public static String doPostSoap(String postUrl, String soapXml, String soapAction, String node, String childNode) {String retStr = "";// 创建HttpClientBuilderHttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// HttpClientCloseableHttpClient closeableHttpClient = httpClientBuilder.build();HttpPost httpPost = new HttpPost(postUrl);// 设置请求和传输超时时间RequestConfig requestConfig = RequestConfig.custom().build();httpPost.setConfig(requestConfig);try {httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");httpPost.setHeader("SOAPAction", soapAction);StringEntity data = new StringEntity(soapXml, StandardCharsets.UTF_8);httpPost.setEntity(data);CloseableHttpResponse response = closeableHttpClient.execute(httpPost);HttpEntity httpEntity = response.getEntity();if (httpEntity != null) {// 打印响应内容retStr = EntityUtils.toString(httpEntity);System.out.println("soap响应内容 ===> " + retStr);}// 释放资源closeableHttpClient.close();} catch (Exception e) {}return soapXmlToBean(retStr, node, childNode);}/*** @param soapXml 请求结果string* @return 请求结果*/public static String soapXmlToBean(String soapXml, String node, String childNode) {Iterator<SOAPElement> iterator = null;String res = "";try {//javax.xml.soap类MessageFactoryMessageFactory msgFactory = MessageFactory.newInstance();//创建一个soapMessage对象SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),new ByteArrayInputStream(soapXml.getBytes(StandardCharsets.UTF_8)));reqMsg.saveChanges();//取出soapBody对象SOAPBody body = reqMsg.getSOAPBody();//遍历子节点iterator = body.getChildElements();} catch (Exception e) {e.printStackTrace();}while (iterator.hasNext()) {SOAPElement element = iterator.next();System.out.println("节点名称---:" + element.getNodeName());if (node.equals(element.getNodeName())) {Iterator<SOAPElement> it = element.getChildElements();SOAPElement el = null;while (it.hasNext()) {el = it.next();//取到content子节点的值if (childNode.equals(el.getNodeName())) {System.out.println("子节点值---:" + el.getValue());res = el.getValue();break;}}break;}}return res;}
}
拼接完成后的请求参数
03fd0ac1177fd44a98761e16a920bb55
在里面测试一下,标签里面包裹的就是请求的数据,然后再调用.()把数据提出来,更完美的写法可以参考这个【链接】
参考链接