当前位置: 首页 > 知识库问答 >
问题:

用Java发送Soap请求[重复]

巩光誉
2023-03-14

我从来没有用过肥皂。我搜索了一下,找到了一些例子。

我的目标是发送这样的肥皂请求

<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 idetificator</tem:paymentId>
     <tem:data>
        <tem:param>
           <tem:key>account</tem:key>
           <tem:value>account cridentials</tem:value>
        </tem:param>
     </tem:data>
     <tem:hash>?</tem:hash>
   </tem:pay>
 </soapenv:Body>
</soapenv:Envelope>

有人能告诉我如何发送这样的肥皂请求吗?或者给我一个例子或教程来发送这样的肥皂。谢谢大家。

共有1个答案

澹台博文
2023-03-14

下面有一个演示,告诉你如何去做。基本上,您可以为每个需要的元素调用< code>addChildElement和< code>addTextNode。

在调用之前,请确保在main方法中更改endpointURL和SOAP操作。

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    // SAAJ - SOAP Client Testing
    public static void main(String args[]) {
        String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx"; // CHANGE ME
        String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit"; // CHANGE ME

        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "tem";
        String myNamespaceURI = "http://tempuri.org/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("pay", myNamespace);

        SOAPElement merchantId = soapBodyElem.addChildElement("merchantId", myNamespace);
        merchantId.addTextNode("7507231");

        SOAPElement branch = soapBodyElem.addChildElement("branch", myNamespace);
        branch.addTextNode("Licensed Branch Name");

        SOAPElement alias = soapBodyElem.addChildElement("alias", myNamespace);
        alias.addTextNode("Service alias Name");

        SOAPElement paymentId = soapBodyElem.addChildElement("paymentId", myNamespace);
        paymentId.addTextNode("merchants payment idetificator");

        SOAPElement data = soapBodyElem.addChildElement("data", myNamespace);
        SOAPElement dataParam = data.addChildElement("param", myNamespace);
        SOAPElement dataParamKey = dataParam.addChildElement("key", myNamespace); dataParamKey.addTextNode("account");
        SOAPElement dataParamValue = dataParam.addChildElement("value", myNamespace); dataParamValue.addTextNode("account cridentials");

        SOAPElement hash = soapBodyElem.addChildElement("hash", myNamespace);
        hash.addTextNode("?");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapRequest = createSOAPRequest(soapAction);
            SOAPMessage soapResponse = soapConnection.call(soapRequest, soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

}
 类似资料:
  • 问题内容: 是否可以使用Python的库发送SOAP请求? 问题答案: 确实有可能。 这是一个使用普通请求lib调用Weather SOAP Service的示例: 一些注意事项: 标头很重要。没有正确的标头,大多数SOAP请求将无法工作。可能是更 正确 使用的标头(但weatherservice更喜欢 这将以xml字符串形式返回响应-然后,您需要解析该xml。 为简单起见,我以纯文本形式包含了该

  • 问题内容: 我正在尝试向SOAP Web服务发送请求。我阅读了本教程并准备了以下代码。但是,我将向多个SOAP Web服务发送不同的请求,而本教程只关注一个请求。如何使用发送SOAP请求? WebServiceTemplate 问题答案: 您可以使用以下代码,而无需在xml文件中定义任何内容。

  • 好的,我对网络服务完全陌生,对于我正在做的一个项目,我试图了解整个SOAP。我想我对正在发生的事情有一个模糊的理解,但是我缺少一些具体的信息,我在谷歌上找不到任何有用的东西。 我已经阅读了其他人提出的问题,例如使用java向Web服务发出的SOAP请求,但我仍然无法完全弄清楚发生了什么。 具体来说,我尝试使用这里提供的服务http://ec.europa.eu/taxation_customs/v

  • 问题内容: 让我们假设这个网址… (此处的ID需要在POST请求中发送) 我想将其发送到服务器的,该服务器在POST方法中接受它。 如何在Java中执行此操作? 我尝试了这个: 但是我仍然不知道如何通过POST发送 问题答案: 由于原始答案中的某些类已在Apache HTTP Components的较新版本中弃用,因此,我将发布此更新。 顺便说一句,你可以在此处访问完整的文档以获取更多示例。

  • 有史以来第一次使用PS,有史以来第一次一般的脚本! 我在主机上本地存储了一个 wsdl,它指向本地主机终结点。这是必要的,因为远程服务器不为 wsdl 提供服务。但是,它们是这些 SOAP 请求的有效终结点。 我的网络中有多个endpoint,我想在引用静态wsdl的同时向每个endpoint发送New-WebServiceProxy请求。是否有办法使用本地wsdl,但指定目标endpoint覆盖

  • 很抱歉,如果这个问题是重复的,但是我在StackOverflow中尝试了几个解决方案,仍然无法发送SOAP请求。 我目前的情况是我的java应用程序需要向我的客户Web服务发送SOAP请求,这是我在SOAP UI中成功完成的,用户名,密码,身份验证类型:抢先。请看图片 现在我想在Java这样做,我已经创建了SOAP信封,没有设置密码,响应与SOAP UI中使用无身份验证完全相同,这意味着java代