Httpwebresponse.cookies returning inconsistant values.


G

Guest

Hi ,

i am trying to pass the same session Id to all the webrequest, but
sometimes the response.cookies returns zero and sometimes one. is this to do
something with cookies expire. In this sample code the line

Console.WriteLine("cookies1 count : " + firstResponse.Count.ToString()); and
Console.WriteLine("second Cookie : " +
secondResponse.Cookies.Count.ToString());

print different values (0 or 1) each time the sample code is complied and
runned. Any help and suggestion will be a great help forme.
The Sample code is as follows.


HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://localhost/Test/WebForm1.aspx?usr=test&pwd=test");
string postData = @"usr=test&pwd=test";
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);

req.ContentType = @"application/x-www-form-urlencoded";

req.Method="POST";
req.ContentLength = postData.Length;
req.KeepAlive=true;
Stream reqStream = req.GetRequestStream();
reqStream.Write(byte1,0,byte1.Length);
reqStream.Close();

req.CookieContainer = new CookieContainer();
HttpWebResponse firstResponse = (HttpWebResponse)req.GetResponse();
Console.WriteLine("first response : " + firstResponse.StatusCode.ToString());
Console.WriteLine("cookies1 count : " + firstResponse.Count.ToString());

HttpWebRequest secondRequest = (HttpWebRequest)
WebRequest.Create(@"http://localhost/Test/WebForm2.aspx");

secondRequest.ContentType = @"application/x-www-form-urlencoded";
secondRequest.Method="POST";
secondRequest.ContentLength = postData.Length;
secondRequest.KeepAlive=true;
reqStream = secondRequest.GetRequestStream();
reqStream.Write(byte1,0,byte1.Length);
reqStream.Close();
secondRequest.KeepAlive=true;
secondRequest.CookieContainer = new CookieContainer();
CookieCollection cookies1 = firstResponse.Cookies;

secondRequest.CookieContainer.Add(cookies1);

HttpWebResponse secondResponse = (HttpWebResponse)secondRequest.GetResponse();

Console.WriteLine("second Response : " +
secondResponse.StatusCode.ToString());

Console.WriteLine("second Cookie : " +
secondResponse.Cookies.Count.ToString());
 
Ad

Advertisements

J

Joerg Jooss

Ravi said:
Hi ,

i am trying to pass the same session Id to all the webrequest, but
sometimes the response.cookies returns zero and sometimes one. is
this to do something with cookies expire. In this sample code the line

Console.WriteLine("cookies1 count : " +
firstResponse.Count.ToString()); and Console.WriteLine("second
Cookie : " + secondResponse.Cookies.Count.ToString());

print different values (0 or 1) each time the sample code is complied
and runned. Any help and suggestion will be a great help forme.
The Sample code is as follows.


HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://localhost/Test/WebForm1.asp
x?usr=test&pwd=test"); string postData = @"usr=test&pwd=test";
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);

req.ContentType = @"application/x-www-form-urlencoded";

req.Method="POST";
req.ContentLength = postData.Length;
req.KeepAlive=true;
Stream reqStream = req.GetRequestStream();
reqStream.Write(byte1,0,byte1.Length);
reqStream.Close();

req.CookieContainer = new CookieContainer();
HttpWebResponse firstResponse = (HttpWebResponse)req.GetResponse();
Console.WriteLine("first response : " +
firstResponse.StatusCode.ToString()); Console.WriteLine("cookies1
count : " + firstResponse.Count.ToString());

HttpWebRequest secondRequest = (HttpWebRequest)
WebRequest.Create(@"http://localhost/Test/WebForm2.aspx");

secondRequest.ContentType = @"application/x-www-form-urlencoded";
secondRequest.Method="POST";
secondRequest.ContentLength = postData.Length;
secondRequest.KeepAlive=true;
reqStream = secondRequest.GetRequestStream();
reqStream.Write(byte1,0,byte1.Length);
reqStream.Close();
secondRequest.KeepAlive=true;
secondRequest.CookieContainer = new CookieContainer();
CookieCollection cookies1 = firstResponse.Cookies;

secondRequest.CookieContainer.Add(cookies1);

HttpWebResponse secondResponse =
(HttpWebResponse)secondRequest.GetResponse();
Console.WriteLine("second Response : " +
secondResponse.StatusCode.ToString());

Console.WriteLine("second Cookie : " +
secondResponse.Cookies.Count.ToString());

You only need one CookieContainer instance. CookieContainer keeps track
of all cookies automatically, so there's no reason to copy cookies from
the response to the container.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top