Session lost for return URL of GetResponse()

767 Views Asked by At

I am making a WebRequest from Page1 to Page2. Page2 process the information and sends the URL link(http://xyz/abc/Page3.aspx) in response to Page1.

Page2 and Page3 belong to same solution and share session variables.

In Page1, once it receive the response. It picks the URL sent by page2 and tries to go it. But unfortunately session is lost in Page3. I was expecting that the session variables stored in Page2 to be accessible in Page3.

In Page1: WebRequest request = WebRequest.Create("page2");
...
request.GetResponse();
...
String urlStr=getURL();
//This url has link to Page3
Response.Redirect(urlStr);

In Page2: Session["guid"]="stackoverflow";
...
Context.Response.Write(xmlString);
Context.Response.End();

In Page3: //Here is I am loosing the session value
String temp= Convert.ToString(Session["guid"]);

UPDATE: Solution
In Page1: request.CookieContainer = new CookieContainer();
..
System.Net.WebResponse response = request.GetResponse();
CookieCollection respCookieCollection= ((HttpWebResponse)response).Cookies;

...
//Create new request to navigate to Page3 with cookies got from previous response. HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create(url); request2.Method = WebRequestMethods.Http.Post; request2.CookieContainer = new CookieContainer(); request2.CookieContainer.Add(respCookieCollection);
.... System.Net.WebResponse response2 = request2.GetResponse(); System.IO.StreamReader reader2 = new System.IO.StreamReader(response2.GetResponseStream()); string str2 = reader2.ReadToEnd(); Response.Write(str2);

0

There are 0 best solutions below