gpt4 book ai didi

java - 如何为给定 xml 的客户端 saaj 创建 SOAP 请求

转载 作者:行者123 更新时间:2023-11-30 06:49:24 34 4
gpt4 key购买 nike

我是 SOAP 的新手。我想创建一个以下 xmlsoap 请求。

xml

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<request xsi:type="RegisterCheckRequest" Id="7a646d45-ee2f-4b1c-8de8-780c416fbbd0" Service="42" xmlns="http://paygo24.com/v3/protocol">
<PaymentParameters xmlns="">
<Parameter Name="account" Value="08374829" />
</PaymentParameters>
</request>
<pointId>46</pointId>
<password>4QrcOUm6Wau+VuBX8g+IPg==</password>
</SendRequest>
</soap:Body>
</soap:Envelope>

我正在使用下面的示例java文件来创建soap请求,但我无法这样做,任何人都可以给我支持。

用于创建 SOAP 请求的示例 Java 文件

import javax.xml.soap.*;

public class SOAPClientSAAJ {

public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);

soapConnection.close();
}

private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

String serverURI = "http://ws.cdyne.com/";

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);

/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<example:VerifyEmail>
<example:email>mutantninja@gmail.com</example:email>
<example:LicenseKey>123</example:LicenseKey>
</example:VerifyEmail>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
soapBodyElem1.addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
soapBodyElem2.addTextNode("123");

MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "VerifyEmail");

soapMessage.saveChanges();

/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();

return soapMessage;
}

}

最佳答案

这就是构建 SOAP 请求的方式

 private static SOAPMessage createSOAPRequest() throws Exception {

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();


SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("soap","http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
SOAPHeader header=soapMessage.getSOAPHeader();
header.setPrefix("soap");
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement root=soapBody.addChildElement(new QName("http://tempuri.org/","SendRequest"));
SOAPElement request=root.addChildElement(new QName("http://paygo24.com/v3/protocol","request"));
request.setAttribute("xsi:type", "RegisterCheckRequest");
request.setAttribute("Id","7a646d45-ee2f-4b1c-8de8-780c416fbbd0");
request.setAttribute("Service","42");
SOAPElement paymentParameters =request.addChildElement(new QName(" ","PaymentParameters"));
SOAPElement parameter=paymentParameters.addChildElement("Parameter");
parameter.setAttribute("Name","account");
parameter.setAttribute("Value", "08374829");
root.addChildElement("pointId").setValue("46");
root.addChildElement("password").setValue("4QrcOUm6Wau+VuBX8g+IPg==");
soapMessage.saveChanges();

soapMessage.writeTo(System.out);


return soapMessage;
}

关于java - 如何为给定 xml 的客户端 saaj 创建 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43113862/

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