【问题标题】:SAAJ java client - add authenticationSAAJ java客户端——添加认证
【发布时间】:2018-07-22 15:43:45
【问题描述】:

我正在用 SAAJ java 制作一个 web 服务客户端。 我是客户,我询问网络服务的信息。但网络服务受用户名和密码保护。

我不知道我必须如何添加这 2 个。

我在我的代码中尝试过(见命令)但没有结果..

// SAAJ - SOAP 客户端测试 公共静态 void main(String args[]) {

    String soapEndpointUrl = "https://gtstvs01:8443/aeosws";
    String soapAction = "";

    callSoapWebService(soapEndpointUrl, soapAction);
}

private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String myNamespace = "sch";
    String myNamespaceURI = "http://www.nedap.com/aeosws/schema";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);



    //SOAPFactory soapFactory = SOAPFactory.newInstance();
   // SOAPElement authHeaderElement = soapFactory.createElement("AuthenticationHeader", "administrator", "aeosrules");
    //SOAPElement sessionIdElement = soapFactory.createElement("SessionID", "nsprefix", "nsuri");
    //sessionIdElement.addTextNode(MY_SESSION_ID);
    //authHeaderElement.addChildElement(sessionIdElement);

    //SOAPHeader soapHeader = envelope.addHeader();
    //soapHeader.addChildElement(authHeaderElement);


    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("EmployeeSearchInfo", myNamespace);
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("EmployeeInfo", myNamespace);
    SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("FirstName", myNamespace);
    soapBodyElem2.addTextNode("Jens");
}

private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();


        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        // Print the SOAP Response
        System.out.println("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        System.out.println();

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    createSoapEnvelope(soapMessage);

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);

    soapMessage.saveChanges();

    /* Print the request message, just for debugging purposes */
    System.out.println("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println("\n");

    return soapMessage;
}

【问题讨论】:

    标签: java web-services saaj


    【解决方案1】:

    据我了解,您必须将其添加到标题中。

    在您的 CreateSoapRequest 代码中,您需要另外 3 行。

    MimeHeaders headers = soapMessage.getMimeHeaders();
    String encoded = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
    String authString = "Basic " + encoded;
    headers.addHeader("Authorization", authString);
    headers.addHeader("SOAPAction", soapAction);
    

    我认为网站的编码类型很重要。所以这可能在剪切和粘贴中不起作用,您必须弄清楚它需要什么。

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 2010-12-12
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多