gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:38:28 28 4
gpt4 key购买 nike

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

我不知道如何添加这两个。

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

//SAAJ - SOAP 客户端测试 公共(public)静态无效主(字符串参数[]){

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

我认为网站的编码类型很重要。因此,这可能无法通过剪切和粘贴来实现,您必须弄清楚它需要什么。

关于java - SAAJ java 客户端 - 添加身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com