473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Httpwebresponse.cookies returning inconsistant values.

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());
--
ravib
Nov 17 '05 #1
1 4183
Ravi wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Evanchik | last post by:
Hello all, since i wanted to use ssl and its seems easy to do so with this object. Im trying to login to a webserver (aol) for this example. But for some reason, im packet sniffing with ethreal...
3
by: C# Learner | last post by:
The following code gets the *body* of an HTTP message: <code> response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(),...
4
by: Tom Jones | last post by:
Hi, I place an http request to a web server using HttpWebRequest. When the response comes back (via a HttpWebResponse) the Cookies collection in the response is empty, though if I scan through...
3
by: Thaynann | last post by:
I ham tryin to access a website, the first time i send a GET to display the first page it displays the HTML code, but always with text "Enable Cookies In Your Browser", is there a way to have it...
1
by: Prasanna Padmanabhan | last post by:
I am writing a simple HTTP Client in .NET. I make an HTTP Request and examine the response. I get an empty CookieCollection when I do HttpWebResponse.Cookies. However HttpWebResponse.Headers...
16
by: Cheung, Jeffrey Jing-Yen | last post by:
I have a windows form application that generates a request, downloads an image, and waits the user to enter in login info. Unfortunately, this image is dynamic and based on session data. I have...
2
by: Mr Flibble | last post by:
Hey! I get a 404 from a website (this is "correct behaviour") and I'm interested in the value in the cookie "SMSESSION". The question is the following code generates an exception due to the...
2
by: GHS | last post by:
I have some code to connect to a website and pull some content out of the HTML. I've verified that the 2 URLs I'm using are perfectly fine in Internet Explorer and both of them return results...
5
by: piedpiper | last post by:
I am coding a http proxy in c# and using httpwebrequest class to send request to the web server, when the server returns multiple set-cookie headers then the httpwebresponse object combines them all...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.