使用java代码发送SOAP请求

废话不多说 直接上代码
有问题的话自己改吧改吧就行

第一步: 引入pom文件

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

代码实现



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Description 发送SOAP消息
 * @Author MrC
 * @Date 2024/01/11
 **/
public class SoapUtils {

    public static void sendMessage( String url, String soapXml) {

		/*// 消息体格式根据自己的内容写
        String soapXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<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/\">\n" +
                "  <soap:Body>\n" +
                "    <soap:SendTemplateMsg xmlns:soap=\"http://passport.nchu.edu.cn/\">\n" +
                "      <soap:SiteID>XXXX</soap:SiteID>\n" +
                "      <soap:UID>XXXX</soap:UID>\n" +
                "      <soap:WeiXinTemplateID>XXXX</soap:WeiXinTemplateID>\n" +
                "      <soap:Timespan>XXXX</soap:Timespan>\n" +
                "      <soap:SignText>XXXX</soap:SignText>\n" +
                "      <soap:msgbody>XXXX</soap:msgbody>\n" +
                "      <soap:url>XXXX</soap:url>\n" +
                "    </soap:SendTemplateMsg>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>\n";	*/
                
			// 添加请求头
        String headers = "text/xml; charset=utf-8";

        try {
            URL endpoint = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", headers);
            connection.setDoOutput(true);

            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(soapXml.getBytes());
            outputStream.flush();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // 解析SOAP响应
                res = parseSoapResponse(response.toString());
            } else {
                System.out.println("Error sending SOAP request. Response Code: " + responseCode);

                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();

                // 打印响应的XML
                System.out.println("SOAP Response:");
                System.out.println(response.toString());

                // 解析SOAP响应
                parseSoapResponse(response.toString());
            }
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void parseSoapResponse(String soapResponse) {
        try {
            // 创建一个新的 DocumentBuilder 对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 将 SOAP 响应转换为输入流进行解析
            ByteArrayInputStream input = new ByteArrayInputStream(soapResponse.getBytes());

            // 解析输入流并获取 Document 对象
            Document document = builder.parse(input);

            // 获取根元素
            Element rootElement = document.getDocumentElement();

            // 查找 <SendTemplateMsgResult> 元素  这里根据实际响应获取:SendTemplateMsgResult
            NodeList nodeList = rootElement.getElementsByTagName("SendTemplateMsgResult");

            // 提取文本内容
            if (nodeList.getLength() > 0) {
                Element resultElement = (Element) nodeList.item(0);
                String resultValue = resultElement.getTextContent();
                System.out.println("SendTemplateMsgResult: " + resultValue);
            } else {
                System.out.println("SendTemplateMsgResult not found in the SOAP response.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注意:如果XML里带URL链接的 里面的 & 要进行转意 &amp;

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值