我从未使用过肥皂。我搜索并找到了一些示例
我的目标是像肥皂请求一样发送
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:pay>
<tem:merchantId>7507231</tem:merchantId>
<tem:branch>Licensed Branch Name</tem:branch>
<tem:alias>Service alias Name</tem:alias>
<tem:paymentId>merchants payment idetificator</tem:paymentId>
<tem:data>
<tem:param>
<tem:key>account</tem:key>
<tem:value>account cridentials</tem:value>
</tem:param>
</tem:data>
<tem:hash>?</tem:hash>
</tem:pay>
</soapenv:Body>
</soapenv:Envelope>
谁能告诉我如何像肥皂请求一样发送?或给我一个示例或教程,以发送这样的肥皂。谢谢大家
在下面有一个演示您可以做到的。基本上,您需要为您需要的每个元素致电addChildElement
和addTextNode
。
确保您在调用main
方法中更改端点URL和SOAP Action 。
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx"; // CHANGE ME
String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit"; // CHANGE ME
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "tem";
String myNamespaceURI = "http://tempuri.org/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("pay", myNamespace);
SOAPElement merchantId = soapBodyElem.addChildElement("merchantId", myNamespace);
merchantId.addTextNode("7507231");
SOAPElement branch = soapBodyElem.addChildElement("branch", myNamespace);
branch.addTextNode("Licensed Branch Name");
SOAPElement alias = soapBodyElem.addChildElement("alias", myNamespace);
alias.addTextNode("Service alias Name");
SOAPElement paymentId = soapBodyElem.addChildElement("paymentId", myNamespace);
paymentId.addTextNode("merchants payment idetificator");
SOAPElement data = soapBodyElem.addChildElement("data", myNamespace);
SOAPElement dataParam = data.addChildElement("param", myNamespace);
SOAPElement dataParamKey = dataParam.addChildElement("key", myNamespace); dataParamKey.addTextNode("account");
SOAPElement dataParamValue = dataParam.addChildElement("value", myNamespace); dataParamValue.addTextNode("account cridentials");
SOAPElement hash = soapBodyElem.addChildElement("hash", myNamespace);
hash.addTextNode("?");
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapRequest = createSOAPRequest(soapAction);
SOAPMessage soapResponse = soapConnection.call(soapRequest, soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("nError occurred while sending SOAP Request to Server!nMake sure you have the correct endpoint URL and SOAPAction!n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("n");
return soapMessage;
}
}