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

SAAJ java客户端-添加身份验证,java,web-services,saaj,Java,Web Services,Saaj,我正在用saajjava制作一个webservice客户端。 我是客户,我询问网络服务的信息。但是webservice是由用户名和密码保护的 我不知道我该怎么加上这两个 我在代码see命令中尝试了它,但没有结果 //SAAJ-SOAP客户端测试 公共静态字符串参数[]{ String soapEndpointUrl = "https://gtstvs01:8443/aeosws"; String soapAction = ""; callSoapWebService(s

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

我不知道我该怎么加上这两个

我在代码see命令中尝试了它,但没有结果

//SAAJ-SOAP客户端测试 公共静态字符串参数[]{

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

据我所知,您必须将其添加到标题中

在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);
我认为网站的编码类型很重要,所以这在剪切粘贴中可能不起作用,你必须弄清楚它需要什么