How to manually add a cookie to a webservice call in Java?

心已入冬 提交于 2019-12-11 06:54:25

问题


I want to connect to a webservice (WS). However, a cookie must be provided in order to interact with this webservice.

So far, here is what I have:

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...

How can I add the required cookie to the underlying HTTP request to WS?


回答1:


You can do that by adding the corresponding Cookie HTTP header to the message (exactly as you are already doing for the SOAPAction header):

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


来源:https://stackoverflow.com/questions/41239732/how-to-manually-add-a-cookie-to-a-webservice-call-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!