SAAJ Java客户端 - 添加身份验证



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

我不必如何添加这2个。

我在代码中尝试了它(请参阅命令(,但没有结果。

//SAAJ-肥皂客户测试 公共静态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;
}

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

在您的创建代码中,您需要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);

我认为该网站具有哪种编码类型。因此,这可能无法在剪切和粘贴中起作用,您必须弄清楚它的要求。

相关内容

  • 没有找到相关文章

最新更新