使用SAAJ错误的Java客户端:严重:SAAJ0008:错误响应;擅自

时间:2016-07-18 17:16:17

标签: java web-services saaj

我使用SAAJ为Primevera P6 webservices编写了一个Java客户端代码。我收到以下身份验证错误。我正在为代码提供http用户名+密码,但它给出了错误:严重:SAAJ0008:错误的响应;未经授权。有人可以帮我解决这个问题。很久以来我一直困在这个问题上。 Web服务的wsdl是:https://sunpower-p6.oracleindustry.com/p6ws-token/services/ProjectService?wsdl

错误: 请求SOAP消息= 11106 2016年7月18日下午1:03:19 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post 严重:SAAJ0008:反应不佳;擅自 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:响应错误:(401Unauthorized

我的代码:

public class TestClient {

    /*
     Method used to create the SOAP Request
    */
    private static SOAPMessage createSOAPRequest() throws Exception {

        // Next, create the actual message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage    soapMessage    = messageFactory.createMessage();
        SOAPPart       soapPart       = soapMessage.getSOAPPart();


        String serverURI = "http://xmlns.oracle.com/Primavera/P6/WS/Project/V2";

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

        //start: setting HTTP headers - optional, comment out if not needed
        String      authorization = Base64Coder.encodeString("xyz:abc");
        MimeHeaders hd            = soapMessage.getMimeHeaders();
        hd.addHeader("Authorization", "Basic " + authorization);
        //end: setting HTTP headers

        // Create and populate the body
        SOAPBody soapBody = envelope.getBody();

        // Create the main element and namespace
        SOAPElement soapBodyElem  = soapBody.addChildElement("ReadProjects", "ProjectService");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Field", "ProjectService");
        SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("Id", "ProjectService");
        soapBodyElem2.addTextNode("11106");

        hd.addHeader("SOAPAction", serverURI + "ReadProjects");

        // Save the message
        soapMessage.saveChanges();

        // Check the input
        System.out.println("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        // Create the transformer
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer        transformer        = transformerFactory.newTransformer();

        // Extract the content of the reply
        Source sourceContent = soapResponse.getSOAPPart().getContent();

        // Set the output for the transformation
        System.out.println("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
        System.out.println();
    }

    public static void main(String[] args) throws Exception {

        try {
            // First create the connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection        soapConnection        = soapConnectionFactory.createConnection();
            //System.out.println(soapConnection);

            //Send SOAP Message to SOAP Server
            String url = "https://sunpower-p6.oracleindustry.com/p6ws-token/services/ProjectService?wsdl";
            // Send the message and get the reply
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我意识到这是一个古老的问题,但是希望在未来的任何人身上留下一些帮助(就像我一样)。我最近经历过一些可能的情况。

  1. 检查您的详细信息是否正确,或者是否可以建立连接。在继续前进之前要100%确定。
  2. 如果没有为某些设置正确配置数据库(例如,在JBoss上),您将收到未经授权的错误。在我的情况下,数据源文件在尝试连接时未正确读取,导致客户端出现Unauthorized错误。