java - 如何在 Java 中手动将 cookie 添加到 Web 服务调用中?

标签 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 请求中?

最佳答案

您可以通过将相应的 Cookie HTTP header 添加到消息中来实现此目的(与您已经对 SOAPAction header 所做的操作完全相同):

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

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

相关文章:

web-services - 为外部站点(不使用 OAuth)存储用户凭据的智能方法是什么?

web-services - wsimport 未被识别为内部或外部命令

c# - 持久性 Cookie 在浏览器关闭时被删除 - Identity 2.0

asp.net - 在 iFrame 中维护 session 状态

java - 创建 bean 时出错 - JpetStore 示例

java - 检测圆圈与内部矩形之间的碰撞

java - 处理 FTP 列表中的所有消息后停止 Spring Integration 应用程序

java - 使用 Jenkins 构建和 maven 项目进行 Sonar 分析时,无法通过 ClassLoader 警告访问类 'XXX/XXX/XXX '

java - 拦截服务器java中的HTTP请求

javascript - 如何设置临时cookie?