如何手动添加一个cookie到Java的webservice调用? [英] How to manually add a cookie to a webservice call in Java?

查看:764
本文介绍了如何手动添加一个cookie到Java的webservice调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接到Web服务(WS)。

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

如何添加可以通过添加对应的 Cookie

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

推荐答案

/ code> HTTP头到消息(正如你已经为 SOAPAction 头一样):

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);

这篇关于如何手动添加一个cookie到Java的webservice调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆