如何在Java中手动将cookie添加到webservice调用中?

时间:2016-12-20 10:09:37

标签: java web-services cookies soap saaj

我想连接到Web服务(WS)。但是,必须提供cookie才能与此Web服务进行交互。

到目前为止,这就是我所拥有的:

String requiredCookieName = "requiredCookieName";
String requiredCookieValue = getRequiredCookieValue();

// Prepare SOAP message
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
soapMessage.getMimeHeaders().addHeader("SOAPAction", getSoapAction());
soapMessage.saveChanges();

// Send SOAP message
SOAPConnection soapConnection = buildSoapConnection();
SOAPBody soapBody = soapConnection
                      // How to add required cookie here before calling WS?
                      .call(soapMessage, getOperationLocation("operationName"))
                      .getSOAPBody();

// Process response...

如何将所需的cookie添加到WS的基础HTTP请求中?

1 个答案:

答案 0 :(得分:1)

您可以通过在邮件中添加相应的Cookie HTTP标头来完成此操作(与您对SOAPAction标头的操作完全相同):

soapMessage.getMimeHeaders().addHeader(
    "Cookie", requiredCookieName + "=" + requiredCookieValue);