2

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.

Rachel
  • 763
  • 14
  • 29

2 Answers2

4

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;
}
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hi Darin Thanks 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 Sep 24 '09 at 03:54
  • 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 Sep 24 '09 at 07:09
1

This answer is largely based on the answer by Darin Dimitrov - if you find it useful please upvote his answer.

In my case the web service wanted the JSESSIONID value as a cookie, not as a miscellaneous header value.

Also my WCF client was using proxy code generated by Visual Studio's Project - Set Service Reference facility, which I believe is the same as using the wsdl.exe program.

  // 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();
     }
RenniePet
  • 11,420
  • 7
  • 80
  • 106