如何在使用c#从Windows应用程序调用java web服务时在请求中设置cookie。我想在调用java webservice时将HttpHeader中的JSESSIONID作为cookie传递。我有我的JSESSIONID。我想知道如何创建一个cookie并在请求中传递它。如何在从Windows应用程序使用c调用java web服务时在请求中设置cookie#
有人可以建议我。这是否可行?
如何在使用c#从Windows应用程序调用java web服务时在请求中设置cookie。我想在调用java webservice时将HttpHeader中的JSESSIONID作为cookie传递。我有我的JSESSIONID。我想知道如何创建一个cookie并在请求中传递它。如何在从Windows应用程序使用c调用java web服务时在请求中设置cookie#
有人可以建议我。这是否可行?
如果您正在使用WCF生成客户端代理(svcutil.exe的),你可以自定义HTTP标题附加您的要求是这样的:
// MyServiceClient is the class generated by svcutil.exe which derives from
// System.ServiceModel.ClientBase<TServiceContract> and which allows you to
// call the web service methods
using (var client = new MyServiceClient())
using (var scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
// Set the header value
httpRequestProperty.Headers.Add("JSESSIONID", "XXXXX");
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
// Invoke the method
client.SomeMethod();
}
如果您使用Wsdl.exe用生成客户端你可以看看here。
UPDATE:
其实没有必要强制转换为HttpWebRequest的添加自定义标题:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
WebRequest request = base.GetWebRequest(uri);
request.Headers.Add("JSESSIONID", "XXXXX");
return request;
}
嗨达林 感谢您的快速回复。我们正在使用wsdl.exe来生成客户端。我们使用的是c#2003,所以在代理类本身中覆盖了GetWebRequest方法(因为我们在c#2003中没有分类选项)。 我们尝试了链接中给出的解决方案,但在尝试转换为HttpWebRequest时遇到了IllegalCastException。 请让我们知道如何解决这个问题。 – Rachel 2009-09-24 03:54:15
客户端是一个使用c#2003实现的Windows桌面应用程序。是否可以在请求的HttpHeader中将JESSIONID作为cookie传递,同时调用java web服务。请让我们知道,如果有一种方法来实现这一点使用c#2003. – Rachel 2009-09-24 07:09:29
你可以尝试添加标题,而不是铸造。查看我的更新。 – 2009-09-24 07:44:30
这个答案在很大程度上是基于由达林季米特洛夫的答案 - 如果你发现它有用的,请upvote他的答案。
在我的情况下,Web服务希望JSESSIONID值作为cookie,而不是杂项头值。
另外我的WCF客户端使用由Visual Studio的Project-Set Service Reference工具生成的代理代码,我相信它与使用wsdl.exe程序相同。
// Session ID received from web service as response to authentication login
private string _sessionId;
// This code needed to add the session ID to the HTTP header as a JSESSIONID cookie value
using (MyServiceClient myServiceClient = new MyServiceClient())
using (new OperationContextScope(myServiceClient.InnerChannel))
{
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add("Cookie", "JSESSIONID=" + _sessionId);
OperationContext.Current.OutgoingMessageProperties.Add(
HttpRequestMessageProperty.Name, httpRequestProperty);
myServiceClient.someMethod();
}
您是否使用svcutil.exe或wsdl.exe来生成您的客户端代理? – 2009-09-22 12:52:18
我们使用wsdl.exe生成的客户端代理。 – Rachel 2009-09-24 03:51:53