空SOAP响应

时间:2017-10-06 15:30:01

标签: java web-services soap

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

        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;
      }
    }

1 个答案:

答案 0 :(得分:1)

您可能使用了错误的方法。您调用的那个(getContentDescription())将返回“Content-Description”HTTP标头的内容。

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