You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Java发送SOAP请求:零基础实现指定格式请求需求

新手快速上手发送指定格式的SOAP请求

嘿,第一次接触SOAP确实会有点懵,不过你已经找了示例,方向对啦!先把你给出的请求结构补全成完整的SOAP信封,然后给你几个能直接跑的代码示例,帮你快速搞定。

第一步:先确认完整的SOAP请求结构

你给出的请求没写完,我帮你补全了标准的收尾部分,完整的请求XML应该是这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:pay>
            <tem:merchantId>7507231</tem:merchantId>
            <tem:branch>Licensed Branch Name</tem:branch>
            <tem:alias>Service alias Name</tem:alias>
            <tem:paymentId>merchants payment identificator</tem:paymentId>
            <!-- 如果还有其他必填参数(比如支付金额、用户标识等),在这里补充即可 -->
        </tem:pay>
    </soapenv:Body>
</soapenv:Envelope>

这里几个关键部分要记牢:

  • xmlns:soapenv 和 xmlns:tem 是SOAP的核心命名空间,绝对不能修改,服务端靠这个识别请求的格式规范
  • <tem:pay> 是你要调用的具体服务方法名,必须和服务端提供的接口完全对应
  • 里面的merchantId、branch等是方法的入参,要替换成你实际的业务数据

第二步:用代码发送请求(新手友好示例)

示例1:Python(用requests库,最容易上手)

先确保你安装了requests库:

pip install requests

然后直接写代码:

import requests

# 替换成实际的SOAP服务地址(比如常见的.asmx或.wsdl对应端点)
soap_service_url = "http://your-soap-service-domain.com/PaymentService.asmx"

# 构造SOAP请求体,把参数换成你自己的业务数据
soap_request_body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:pay>
            <tem:merchantId>7507231</tem:merchantId>
            <tem:branch>Licensed Branch Name</tem:branch>
            <tem:alias>Service alias Name</tem:alias>
            <tem:paymentId>merchants payment identificator</tem:paymentId>
        </tem:pay>
    </soapenv:Body>
</soapenv:Envelope>
"""

# 设置请求头:SOAP请求必须指定Content-Type为text/xml,SOAPAction按需填写(一般是命名空间+方法名)
request_headers = {
    "Content-Type": "text/xml; charset=utf-8",
    "SOAPAction": "http://tempuri.org/pay"
}

# 发送POST请求
response = requests.post(soap_service_url, data=soap_request_body, headers=request_headers)

# 打印结果
print("请求状态码:", response.status_code)
print("SOAP响应内容:\n", response.text)

示例2:Java(基础HttpURLConnection,不用额外框架)

如果用Java,不用第三方框架的话可以这样写:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class SoapPaymentRequest {
    public static void main(String[] args) {
        try {
            // 替换成实际的SOAP服务地址
            URL serviceUrl = new URL("http://your-soap-service-domain.com/PaymentService.asmx");
            HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection();

            // 配置请求参数
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            connection.setRequestProperty("SOAPAction", "http://tempuri.org/pay");
            connection.setDoOutput(true);

            // 构造SOAP请求体
            String soapBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
                    + "<soapenv:Header/>"
                    + "<soapenv:Body>"
                    + "<tem:pay>"
                    + "<tem:merchantId>7507231</tem:merchantId>"
                    + "<tem:branch>Licensed Branch Name</tem:branch>"
                    + "<tem:alias>Service alias Name</tem:alias>"
                    + "<tem:paymentId>merchants payment identificator</tem:paymentId>"
                    + "</tem:pay>"
                    + "</soapenv:Body>"
                    + "</soapenv:Envelope>";

            // 发送请求
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(soapBody);
            outputStream.flush();
            outputStream.close();

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

            // 打印结果
            System.out.println("请求状态码: " + connection.getResponseCode());
            System.out.println("SOAP响应内容:\n" + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

几个新手必看的注意事项

  • 确认服务地址:一定要替换成实际的SOAP服务端点URL,不确定的话可以看服务提供的WSDL文档
  • SOAPAction检查:有些服务要求必须传SOAPAction头,值一般是http://tempuri.org/方法名,如果服务不需要可以留空
  • 参数正确性:所有入参的值要符合服务端的格式要求,比如merchantId是不是纯数字、有没有长度限制
  • 响应解析:返回的是XML格式,你可以用对应语言的XML解析库(比如Python的xml.etree,Java的DocumentBuilder)来提取需要的结果

内容的提问来源于stack exchange,提问作者BekaKK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:03:07