473,769 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Httpwebresponse .cookies returning inconsistant values.

Hi ,

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

Console.WriteLi ne("cookies1 count : " + firstResponse.C ount.ToString() ); and
Console.WriteLi ne("second Cookie : " +
secondResponse. Cookies.Count.T oString());

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.Cre ate(@"http://localhost/Test/WebForm1.aspx?u sr=test&pwd=tes t");
string postData = @"usr=test&pwd= test";
ASCIIEncoding encoding=new ASCIIEncoding() ;
byte[] byte1=encoding. GetBytes(postDa ta);

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

req.Method="POS T";
req.ContentLeng th = postData.Length ;
req.KeepAlive=t rue;
Stream reqStream = req.GetRequestS tream();
reqStream.Write (byte1,0,byte1. Length);
reqStream.Close ();

req.CookieConta iner = new CookieContainer ();
HttpWebResponse firstResponse = (HttpWebRespons e)req.GetRespon se();
Console.WriteLi ne("first response : " + firstResponse.S tatusCode.ToStr ing());
Console.WriteLi ne("cookies1 count : " + firstResponse.C ount.ToString() );

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

secondRequest.C ontentType = @"applicatio n/x-www-form-urlencoded";
secondRequest.M ethod="POST";
secondRequest.C ontentLength = postData.Length ;
secondRequest.K eepAlive=true;
reqStream = secondRequest.G etRequestStream ();
reqStream.Write (byte1,0,byte1. Length);
reqStream.Close ();
secondRequest.K eepAlive=true;
secondRequest.C ookieContainer = new CookieContainer ();
CookieCollectio n cookies1 = firstResponse.C ookies;

secondRequest.C ookieContainer. Add(cookies1);

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

Console.WriteLi ne("second Response : " +
secondResponse. StatusCode.ToSt ring());

Console.WriteLi ne("second Cookie : " +
secondResponse. Cookies.Count.T oString());
--
ravib
Nov 17 '05 #1
1 4205
Ravi wrote:
Hi ,

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

Console.WriteLi ne("cookies1 count : " +
firstResponse.C ount.ToString() ); and Console.WriteLi ne("second
Cookie : " + secondResponse. Cookies.Count.T oString());

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.Cre ate(@"http://localhost/Test/WebForm1.asp
x?usr=test&pwd= test"); string postData = @"usr=test&pwd= test";
ASCIIEncoding encoding=new ASCIIEncoding() ;
byte[] byte1=encoding. GetBytes(postDa ta);

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

req.Method="POS T";
req.ContentLeng th = postData.Length ;
req.KeepAlive=t rue;
Stream reqStream = req.GetRequestS tream();
reqStream.Write (byte1,0,byte1. Length);
reqStream.Close ();

req.CookieConta iner = new CookieContainer ();
HttpWebResponse firstResponse = (HttpWebRespons e)req.GetRespon se();
Console.WriteLi ne("first response : " +
firstResponse.S tatusCode.ToStr ing()); Console.WriteLi ne("cookies1
count : " + firstResponse.C ount.ToString() );

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

secondRequest.C ontentType = @"applicatio n/x-www-form-urlencoded";
secondRequest.M ethod="POST";
secondRequest.C ontentLength = postData.Length ;
secondRequest.K eepAlive=true;
reqStream = secondRequest.G etRequestStream ();
reqStream.Write (byte1,0,byte1. Length);
reqStream.Close ();
secondRequest.K eepAlive=true;
secondRequest.C ookieContainer = new CookieContainer ();
CookieCollectio n cookies1 = firstResponse.C ookies;

secondRequest.C ookieContainer. Add(cookies1);

HttpWebResponse secondResponse =
(HttpWebRespons e)secondRequest .GetResponse();
Console.WriteLi ne("second Response : " +
secondResponse. StatusCode.ToSt ring());

Console.WriteLi ne("second Cookie : " +
secondResponse. Cookies.Count.T oString());


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.d e
Nov 17 '05 #2

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

Similar topics

0
1762
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 and cookies are not being sent along with the header and post data. here is the code im using. Can anyone please tell me why this is happening? I do not want to go back to using VB6 and the inet control!!! If you notice in my class below, i...
3
15871
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(), Encoding.UTF8); string html = sr.ReadToEnd(); </code>
4
5754
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 the response's Headers collection I see that there *are* a number of "Set-Cookie" headers present. Later on in the process I make another request (not to the same Uri though) and the response DOES get its Cookies collection populated correctly.
3
1832
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 not display that and display the actual page, i did get it at some stage, ut that involved a bit of recursion, by callin the method again (from within itself) and that allowed the page to be displayed. But when i have tried to call a POST...
1
1801
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 returns the correct list of cookies (represented as a comma-separated string) in the response. Can someone please explain the discrepancy? Thanks,
16
11056
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 read documents on the CookieCollection property of HttpWebRequest. Currently, I have the functionality in my code to be able to accept cookies, and return them upon a new HttpWebRequest; however, upon further inspection of the returning...
2
3399
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 404. How am I still able to read the cookies even though I get an exception? Here's the code:
2
9695
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 "pretty quickly". The first URL I try takes a while but eventually comes back with the correct results. It took *much longer* than in Internet Explorer. The second URL times out even if I increase the timeout value significantly. Internet explorer...
5
10866
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 in to a single set-cookie key-value pair. I am tryin to get the value of the headers in the following way. for (int i = 0; i < response.Headers.Count; ++i) Console.WriteLine("{0}:{1}", response.Headers.Keys, response.Headers);
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.