473,836 Members | 1,455 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 4208
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
1765
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
15874
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
5757
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
1835
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
1804
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
11059
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
3402
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
9700
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
10867
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
9825
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
9671
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
10852
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
10257
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...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6980
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.