繁体   English   中英

使用Java消费SOAP Web服务(具有安全性)

[英]Consume SOAP web service (having security) in Java

我想将以下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>

我正在使用Java8。我试图在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();
        }
    }

}

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

无法看到服务器端,我看不到如何提供帮助。

SOAP-ENV:Server服务器出现问题,因此消息无法继续。

不幸的是,您将需要查看服务器日志以确定正在发生的事情。

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

我遇到了完全相同的问题,但是在另一个平台上集成了。 通过检查WSDL XML Web服务的SOAP Action来解决该问题,该服务正在消耗,复制和粘贴正确的SOAP Action到我的代码中,并且可以正常工作。

问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM