Close this search box.

Send Cookie in HTTP Header WebRequest (ASP.NET C# Web Services)

Send Cookie in HTTP Header WebRequest

Problem: You are using a third-party web service to which you are connecting with your own web service. The third-party web service is divided into two phases. Phase 1 is dependent on nothing i.e. you just pass the parameters and the web service returns the details you would like. Phase 2 though is dependent on the first. The problem is that the second phase — apart from the method parameters — also requests that you pass on the JSessionID value which was returned to you in Phase 1. This value must be sent in a cookie. How can this be done?

Reason: When getting data from large databases it is impossible to save all possible combinations. For example, if you are getting all flights between London and Madrid, the data is being fetched from a database. That is Phase 1. But, the IDs sent to you which can be used to get further details on the particular ID of the response cannot all be saved in a database somewhere as it would be extremely redundant to hold all possible combinations. So instead, a session is created on the server and while that session is active the IDs for your particular search are saved. Temporarily. So in order to get the details in Phase 2 you must notify the server which search you are referring to from Phase 1.

Solution: Obviously, you must first execute Phase 1. From there get the JSessionID which will be passed on to the next Phase. The following is all the code you need to call the web service. In it you will find the code to create the JSessionID cookie highlighted in yellow.

byte[] buffer = Encoding.ASCII.GetBytes(
    "fareId=123456");  // the data you want to send to the web service
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
WebReq.Headers["Cookie"] = "JSESSIONID=4567845226"

    Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);

The most important part of the code is highlighted in yellow. In that line of code we are passing a cookie called JSESSIONID with the value 4567845226. When this request arrives at the web service, the first thing it does is it checks the cookie which was passed in the header. It then fetches the Phase 1 search response and continues Phase 2 from there.

Conclusion: The CookieContainer object does not work as intended when passing this JSessionID value. This is the only effective method which I found to this process. Note that the cookie name ‘JSESSIONID’ was used as an example. Various web services use various names for their SessionId cookie.

posted on Friday, October 10, 2008 2:18 PM

This article is part of the GWB Archives. Original Author: X Lines 1 Goal

Related Posts