使用Java中的SOAP web服务(具有安全性)

使用Java中的SOAP web服务(具有安全性),java,web-services,soap,httpclient,saaj,Java,Web Services,Soap,Httpclient,Saaj,我想将以下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="h

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

xyzUser
xyzPass
1.
我期待以下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>

ERRODR01
订单号无效
但是,我得到了以下响应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。我尝试在两个不同的程序中使用ApacheHttpClient(版本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);
    }
}
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();
        }
    }

}
公共类请求初始化{
公共静态void main(字符串参数[]){
试一试{
//创建SOAP连接
SOAPConnectionFactory SOAPConnectionFactory=SOAPConnectionFactory.newInstance();
SOAPConnection-SOAPConnection=soapConnectionFactory.createConnection();
//向SOAP服务器发送SOAP消息
字符串url=“”;
SOAPMessage soapResponse=soapConnection.call(createSOAPRequest(),url);
//处理SOAP响应
打印soapResponse(soapResponse);
soapConnection.close();
}捕获(例外e){
System.err.println(“向服务器发送SOAP请求时出错”);
e、 printStackTrace();
}
}
私有静态SOAPMessage createSOAPRequest()引发异常{
MessageFactory MessageFactory=MessageFactory.newInstance();
SOAPMessage SOAPMessage=messageFactory.createMessage();
SOAPPart SOAPPart=soapMessage.getSOAPPart();
字符串serverURI=”http:///retail/schema/inventory/orderlookupservice/v1";
SOAPEnvelope=soapPart.getEnvelope();
envelope.addNamespaceDeclaration(“v1”,serverURI);
SOAPHeader=envelope.getHeader();
SOAPElement安全性=
header.addChildElement(“安全性”)、“”、“”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
addAttribute(新的QName(“xmlns:wsu”),”http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement用户名令牌=
addChildElement(“UsernameToken”,“UsernameToken”);
SOAPElement用户名=
usernameToken.addChildElement(“用户名”,即“”);
username.addTextNode(“xyzUser”);
SOAPElement密码=
usernameToken.addChildElement(“密码”,“密码”);
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_编码,“UTF-8”);
soapMessage.saveChanges();
/*打印请求消息*/
System.out.print(“请求SOAP消息=”);
soapMessage.writeTo(System.out);
System.out.println();
返回消息;
}
私有静态void printSOAPResponse(SOAPMessage soapResponse)引发异常{
TransformerFactory TransformerFactory=TransformerFactory.newInstance();
Transformer Transformer=transformerFactory.newTransformer();
sourceContent=soapResponse.getSOAPPart().getContent();
System.out.print(“\n响应SOAP消息=”);
StreamResult结果=新的StreamResult(System.out);
transformer.transform(源内容、结果);
}
}
HTTPClient代码如下所示:-

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);
    }
}
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();
        }
    }

}
公共类PostSOAPRequest{
公共静态字符串post()引发异常{
HttpClient client=HttpClientBuilder.create().build();
HttpPost=新的HttpPost(“”);
字符串xml=“xyzUserxyzPass1”;
HttpEntity=newbytearrayentity(xml.getBytes(“UTF-8”);
后设实体(实体);
HttpResponse response=client.execute(post);
字符串结果=EntityUtils.toString(response.getEntity());
返回结果;
}
公共静态void main(字符串[]args){
试一试{
System.out.println(post());
}捕获(例外e){
e、 printStackTrace();
}
}
}

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

如果看不到服务器端,我看不到如何提供帮助

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


不幸的是,您需要查看服务器日志以确定发生了什么。

我遇到了完全相同的问题,但是在另一个平台集成上。通过检查正在使用的WSDL-xmlweb服务的SOAP操作,将正确的SOAP操作复制并粘贴到我的代码中,问题就解决了,它成功了


注意。

有两种操作级别:1。应用凭据2。发送消息。SAAJ提供了这两步机构。

谢谢。我去检查一下服务器。