tags:

views:

1078

answers:

1

How do i set cookie in the request while invoking a java webservice from a windows application using c#. I want to pass the JSESSIONID as cookie in the HttpHeader while invoking a java webservice. I have the JSESSIONID with me. I want to know how to create a cookie and pass the same in the request.

Could someone suggest me. Is it feasible.

+2  A: 

If you are using WCF to generate your client proxy (svcutil.exe) you could append a custom http header with your request like this:

// 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();
}

If you are using wsdl.exe to generate your client you can take a look here.


UPDATE:

Actually there's no need to cast to HttpWebRequest to add a custom header:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
    WebRequest request = base.GetWebRequest(uri);
    request.Headers.Add("JSESSIONID", "XXXXX");
    return request;
}
Darin Dimitrov
Hi DarinThanks for you quick reply. We are using wsdl.exe to generate the client. We are using c# 2003 and so have overriden the GetWebRequest method in the proxy class itself (since we do not have the partial class option in c# 2003).We tried the solution given in the link but we are getting the IllegalCastException while trying to cast to HttpWebRequest.Please let us how this can be fixed.
Rachel
Client is a windows desktop application implemented using c# 2003. Is it possible to pass JESSIONID as cookie in the HttpHeader of the request while invoking a java webservice. Please let us know if there is a way to achieve this using c# 2003.
Rachel
You could try adding headers without casting. See my update.
Darin Dimitrov
Thanks Darin. It workes :-)
Rachel