java - 空 SOAP 响应

标签 java web-services soap

我编写了以下代码来将 SOAP 请求发送到端点并获取响应。当我运行代码时,我得到空响应。我可以知道我的代码有什么问题吗?提前致谢。当我在 SOAP UI 中发送相同的 header 时,我得到了正确的响应。我觉得需要添加更多逻辑

        package com.siebel.Webservice;

    import java.io.IOException;
    import java.net.URL;

    import javax.xml.namespace.QName;
    import javax.xml.soap.MessageFactory;
    import javax.xml.soap.MimeHeaders;
    import javax.xml.soap.SOAPBody;
    import javax.xml.soap.SOAPBodyElement;
    import javax.xml.soap.SOAPConnection;
    import javax.xml.soap.SOAPConnectionFactory;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.soap.SOAPHeaderElement;
    import javax.xml.soap.SOAPMessage;
    import javax.xml.soap.SOAPPart;

    public class Main {

      public static void main(String[] args) throws Exception {
        SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = sfc.createConnection();

        String endpoint = "https://mywebsite.org/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1";
        SOAPMessage response = connection.call(createSoapEnvelope(), endpoint);
        System.out.println(response.getContentDescription());
      }





      private static SOAPMessage createSoapEnvelope() throws SOAPException {
           MessageFactory messageFactory = MessageFactory.newInstance();
              SOAPMessage soapMessage = messageFactory.createMessage();

          SOAPPart soapPart = soapMessage.getSOAPPart();

          String myNamespace = "arm";
          String myNamespaceURI = "http://siebel.com/Webservice";

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


          //SOAP Header
          SOAPHeader soapheader = envelope.getHeader();

          // Create a header block
          QName username = new QName("http://siebel.com/webservices", "UsernameToken");
          SOAPHeaderElement headerElement = soapheader.addHeaderElement(username);
          headerElement.addTextNode("username");


          QName password = new QName("http://siebel.com/webservices", "PasswordText");
          SOAPHeaderElement headerElement2 = soapheader.addHeaderElement(password);
          headerElement2.addTextNode("password");


          QName session = new QName("http://siebel.com/webservices", "SessionType");
          SOAPHeaderElement headerElement3 = soapheader.addHeaderElement(session);
          headerElement3.addTextNode("Stateless");




          // SOAP Body
          SOAPBody soapBody = envelope.getBody();
          SOAPElement soapBodyElem = soapBody.addChildElement("QueryList_Input", myNamespace);
          SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("SRNum", myNamespace);
          soapBodyElem1.addTextNode("");
          try {
              System.out.println("Soap message is ");
            soapMessage.writeTo(System.out);
            System.out.println();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          return soapMessage;
      }
    }

最佳答案

您可能使用了错误的方法。您调用的 (getContentDescription()) 会返回“Content-Description”HTTP header 的内容。

相反,要获取 SOAPMessage 的文本表示形式,您需要使用一些转换器,例如此处所述: How to convert SOAPBody to String

关于java - 空 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609171/

相关文章:

php - SOAP 身份验证问题

java - java中的方法重载错误

java - 英国国家语料库

web-services - 如何让 Play Framework WS 使用我的 SSLContext

python - Spyne/RPClib - 如何响应 ComplexModel?

java - 将 ws-security 添加到 wsdl2java 生成的类

java - 生成复合 PK null id

java - 解释一下java中的Vector()

java - Hibernate:用继承覆盖sql-delete

c++ - 将带有 XML 命令和大量内存项目数据的 native C++ 服务器移植到 Web 服务。如何做以及使用哪个框架?