gpt4 book ai didi

java - 如何在java中准备SOAP请求并调用Web服务

转载 作者:行者123 更新时间:2023-12-01 12:49:41 24 4
gpt4 key购买 nike

我是 SOAP 新手,我想调用 Web 服务。
我看过一个关于发出 SOAP 请求的教程。
这是代码

    MessageFactory mf = null;
SOAPMessage sm = null;
SOAPEnvelope envelope = null;
MimeHeaders headers = null;
SOAPBody body = null;
SOAPElement requestData = null;
SOAPElement requestDoc = null;
CDATASection cdata = null;
SOAPConnectionFactory sfc = null;
SOAPConnection connection = null;
URL requestUrl = null;
SOAPMessage response = null;
OutputStream os = null;
String host = null;
int port = 80;
String path = null;
String api_key = null;
int socket_timeout = 0;
String service_type = null;
String baseXml = null;

try {

xmlChannelRequest = createChannelRequest(cmId, function, guId, password, estabId);

mf = MessageFactory.newInstance();
sm = mf.createMessage();
envelope = sm.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soapenv");

envelope.setAttribute("xmlns:tem", "http://tempuri.org/");

headers = sm.getMimeHeaders();
headers.addHeader("SOAPAction", "http://tempuri.org/IPublicChannelManagerService/RequestData");

body = envelope.getBody();
body.setPrefix("soapenv");

requestData = body.addChildElement("RequestData");
requestData.setPrefix("tem");

requestDoc = requestData.addChildElement("requestDocument", "tem", "http://tempuri.org/");


cdata = requestDoc.getOwnerDocument().createCDATASection("<Request>\n"
+ " <Authentication CMId=\"6438\" Function=\"31\" Guid=\"5594FB83-F4D4-431F-B3C5-EA6D7ASDSBA795\" Password=\"y656g321TR\"/>\n"
+ " <Establishment Id=\"297867\"/>\n"
+ " </Request>");



requestDoc.appendChild(cdata);

System.out.println("---------------- SOAP Request ----------------------------");

sm.writeTo(System.out);

sfc = SOAPConnectionFactory.newInstance();
connection = sfc.createConnection();
requestUrl = new URL("http://pp.xxx.yyyy.co.uk/zzzz.svc?wsdl");

response = connection.call(sm, requestUrl);

这工作正常。我的问题是,是否有任何简单的方法,就像将对象传递给方法并创建 SOAP 请求一样。
ex : 就像 JaxB 一样,我将发送一个 Bean ,它将生成 xml。

        outputStream = new ByteArrayOutputStream();

marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(beanObject, outputStream);

最佳答案

在发出请求之前,您应该拥有 WSDL 文件。请求。然后只需使用例如生成您的特定类wsimport 工具,然后调用 JAX-WS Web 服务。您不必以这种方式自己构建请求

看看this教程。

关于java - 如何在java中准备SOAP请求并调用Web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329327/

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