【问题标题】:Java client using SAAJ error: SEVERE: SAAJ0008: Bad Response; UnauthorizedJava 客户端使用 SAAJ 错误:严重:SAAJ0008:错误响应;未经授权
【发布时间】:2016-11-21 09:39:54
【问题描述】:

我使用 SAAJ 为 Primevera P6 网络服务编写了 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 帖子 严重: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());
        }

    }
}

【问题讨论】:

    标签: java web-services saaj


    【解决方案1】:

    我意识到这是一个老问题,但我想为将来来到这里的任何人提供一些帮助(就像我一样)。我最近遇到了几种可能的情况。

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

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2018-07-07
      • 1970-01-01
      • 2021-01-07
      • 2022-11-11
      • 2020-03-27
      • 1970-01-01
      • 2016-06-29
      • 2014-03-29
      相关资源
      最近更新 更多