我想连接到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请求中?

最佳答案

您可以通过将相应的Cookie HTTP标头添加到消息中来完成此操作(与您对SOAPAction标头所做的完全相同):

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

关于java - 如何在Java中将cookie手动添加到Web服务调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41239732/

10-10 22:19