Web Services Java客户端使用SAAJ错误:严重度:SAAJ 0008:错误响应;未授权

cyej8jka  于 2022-11-15  发布在  Java
关注(0)|答案(1)|浏览(336)

我已经为Primevera P6 Web服务编写了一个使用SAAJ的Java客户端代码。我收到了以下身份验证错误。我向代码提供了http用户名+密码,但它给出了错误:严重度:SAAJ 0008:错误响应;未授权。有人能帮我解决这个问题吗?我已经被这个问题困扰很久了。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.HttpSOAP连接发布严重:SAAJ 0008:响应错误;未经授权的com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:错误响应:(401未经授权
我的代码:

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

    }
}
tct7dpnv

tct7dpnv1#

我知道这是一个老问题,但我想留下一些帮助,任何人在未来谁结束在这里(像我一样)。有一些可能的情况,我最近经历了关于这一点。
1.请检查您的详细信息是否正确,或者连接是否可行。在继续之前,请100%确定。
1.如果您的数据库在某些设置中没有正确配置(例如,在JBoss上),您将收到一个未授权的错误。在我的情况下,当试图连接时,数据源文件没有被正确读取,导致客户端出现未授权的错误。

相关问题