gpt4 book ai didi

java - 空 SOAP 响应

转载 作者:行者123 更新时间:2023-12-02 12:10:03 26 4
gpt4 key购买 nike

我编写了以下代码来将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com