-6ren">
gpt4 book ai didi

java - 在 Java 中使用 SOAP Web 服务(具有安全性)

转载 作者:行者123 更新时间:2023-11-30 03:25:53 25 4
gpt4 key购买 nike

我想将以下 XML 发布到 Web 服务:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<soapenv:Header>
<Security xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>xyzUser</Username>
<Password>xyzPass</Password>
</UsernameToken>
</Security>
</soapenv:Header>
<soapenv:Body>
<v1:OrderSearchRequest>
<v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber>
</v1:OrderSearchRequest>
</soapenv:Body>
</soapenv:Envelope>

我期待以下响应 XML:-

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<v1:OrderSearchResponse xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
<v1:error>
<v1:errorCode>ERRODR01</v1:errorCode>
<v1:errorMessage>Order Number is Invalid</v1:errorMessage>
</v1:error>
</v1:OrderSearchResponse>
</soapenv:Body>
</soapenv:Envelope>

但是,我收到以下响应 XML,表明存在一些错误:-

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
</env:Header>
<env:Body>
<env:Fault>
<faultcode>env:Server
</faultcode>
<faultstring>
</faultstring>
<detail xmlns:fault="http://www.vordel.com/soapfaults" fault:type="faultDetails">
</detail>
</env:Fault>
</env:Body>
</env:Envelope>

我正在使用 Java 8。我尝试在 2 个不同的程序中使用 Apache HTTPClient(版本 4.4.1)和 SAAJ 进行 POST 操作,但无法修复此问题。有人可以帮忙吗?

SAAJ代码如下:-

public class RequestInitiation {

public static void main(String args[]) {

try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
String url = "<service_endpoint>";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

// Process the SOAP Response
printSOAPResponse(soapResponse);

soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}

private static SOAPMessage createSOAPRequest() throws Exception {

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

String serverURI = "http://<some_site>/retail/schema/inventory/orderlookupservice/v1";

SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("v1", serverURI);

SOAPHeader header = envelope.getHeader();

SOAPElement security =
header.addChildElement("Security", "", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
security.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

SOAPElement usernameToken =
security.addChildElement("UsernameToken", "");

SOAPElement username =
usernameToken.addChildElement("Username", "");
username.addTextNode("xyzUser");

SOAPElement password =
usernameToken.addChildElement("Password", "");
password.addTextNode("xyzPass");

SOAPBody soapBody = envelope.getBody();

SOAPElement soapBodyElem = soapBody.addChildElement("OrderSearchRequest", "v1");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("RETAS400OrderNumber", "v1");
soapBodyElem1.addTextNode("1");

soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

soapMessage.saveChanges();

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

return soapMessage;
}

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}

HTTPClient代码如下:-

public class PostSOAPRequest {

public static String post() throws Exception {

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("<service_endpoint>");
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://<some_site>/retail/schema/inventory/orderlookupservice/v1\"><soapenv:Header><Security xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>xyzUser</Username><Password>xyzPass</Password></UsernameToken></Security></soapenv:Header><soapenv:Body><v1:OrderSearchRequest><v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber></v1:OrderSearchRequest></soapenv:Body></soapenv:Envelope>";
HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
return result;
}

public static void main(String[] args) {
try {
System.out.println(post());
} catch (Exception e) {
e.printStackTrace();
}
}

}

注意:站点名称、端点、用户名和密码在此消息中替换为虚拟值。

最佳答案

发生 2 个级别的操作:1. 应用凭据 2. 发送消息。 SAAJ 中提供了此两步机制。

关于java - 在 Java 中使用 SOAP Web 服务(具有安全性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248746/

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