我想连接到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请求中?
答案 0 :(得分:1)
您可以通过在邮件中添加相应的Cookie
HTTP标头来完成此操作(与您对SOAPAction
标头的操作完全相同):
soapMessage.getMimeHeaders().addHeader(
"Cookie", requiredCookieName + "=" + requiredCookieValue);