473,385 Members | 1,867 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,385 software developers and data experts.

HttpWebRequest usage of cookies

Dan
Question about the environment used by HttpWebRequest when making
requests:

When I use IE to browse a site, all of my cookies, etc. are available
as part of the request. However, when I make that same request using
HttpWebRequest, all of the cookies that I have are not available as
part of the request.

Is there a way to change that - I want all of the cookies that the
site has set to be available. If there is no way to change it, what
are my other options?

Here's a simple repro.

1. Go to this page: http://www.battagin.com/dan/temp/cookiedemo.asp
2. Enter a value in the textbox and click submit. It will be set as
a cookie on your machine, and will display in the page under the
textbox. NOTE: close your browser and re-navigate to the page to
ensure that it persists.
3. Create an HttpWebRequest to the page and look at the HTML that
comes back (code below)
4. Result: The cookie value is not in the HTML.

Thanks,

-Dan

Sample code:

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Net;

namespace CookieDemo {

public class Form1 : Form {
TextBox _txt1 = new TextBox();
TextBox _txt2 = new TextBox();
Button _cmd = new Button();

public Form1() {
_txt1.Location = new Point(5, 5);
_txt1.Size = new Size(315, 20);
_txt1.Text =
"http://www.battagin.com/dan/temp/cookiedemo.asp";

_txt2.Location = new Point(5, 30);
_txt2.Multiline = true;
_txt2.ScrollBars = ScrollBars.Vertical;
_txt2.Size = new Size(400, 230);

_cmd.Location = new Point(330, 5);
_cmd.Text = "Go";
_cmd.Click += new EventHandler(_cmd_Click);

this.Text = "CookieDemo";
this.ClientSize = new Size(412, 266);
this.Controls.AddRange(new Control[] {_cmd,
_txt2, _txt1});

}

[STAThread]
static void Main() {
Application.Run(new Form1());
}

private void _cmd_Click(object sender, EventArgs e) {
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(_txt1.Text);
WebResponse resp = req.GetResponse();
_txt2.Text = new
StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();
}
}
}
Nov 15 '05 #1
4 3487
Hi Dan,
When I use IE to browse a site, all of my cookies, etc. are available
as part of the request. However, when I make that same request using
HttpWebRequest, all of the cookies that I have are not available as
part of the request.


Yes, you have to supply a collection of cookies to be sent with the request.
This means that it is on you to maintain the collection of cookies and
supply it whenever necessary.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #2
Dan
And I take it there is no way to load persistent cookies that IE saved
into my cookies directory?

Thanks
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote in message news:<#c**************@TK2MSFTNGP10.phx.gbl>...
Hi Dan,
When I use IE to browse a site, all of my cookies, etc. are available
as part of the request. However, when I make that same request using
HttpWebRequest, all of the cookies that I have are not available as
part of the request.


Yes, you have to supply a collection of cookies to be sent with the request.
This means that it is on you to maintain the collection of cookies and
supply it whenever necessary.

Nov 15 '05 #3
You can load and parse them manually. The cookies saved by IE are stored as
plain text files, so I think it is pretty possible to load and use them in
your program.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Dan" <tu*******@certes.net> wrote in message
news:67**************************@posting.google.c om...
And I take it there is no way to load persistent cookies that IE saved
into my cookies directory?

Thanks
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote

in message news:<#c**************@TK2MSFTNGP10.phx.gbl>...
Hi Dan,
When I use IE to browse a site, all of my cookies, etc. are available
as part of the request. However, when I make that same request using
HttpWebRequest, all of the cookies that I have are not available as
part of the request.


Yes, you have to supply a collection of cookies to be sent with the request. This means that it is on you to maintain the collection of cookies and
supply it whenever necessary.


Nov 15 '05 #4
You may have a look to this post:
http://www.rendelmann.info/blog/Perm...e-c0fe622dc2e5
to get help about how to get this work for you...

TorstenR

Dmitriy Lapshin [C# / .NET MVP] wrote:
You can load and parse them manually. The cookies saved by IE are
stored as plain text files, so I think it is pretty possible to load
and use them in your program.
"Dan" <tu*******@certes.net> wrote in message
news:67**************************@posting.google.c om...
And I take it there is no way to load persistent cookies that IE
saved into my cookies directory?

Thanks
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com>
wrote

in message news:<#c**************@TK2MSFTNGP10.phx.gbl>...
Hi Dan,

When I use IE to browse a site, all of my cookies, etc. are
available as part of the request. However, when I make that same
request using HttpWebRequest, all of the cookies that I have are
not available as part of the request.

Yes, you have to supply a collection of cookies to be sent with the
request. This means that it is on you to maintain the collection of
cookies and supply it whenever necessary.


--
Regards,
Torsten Rendelmann

PS: Answer/Reply always to the group!
For avoidance of spam mailings my e-mail
address is invalid!
Nov 15 '05 #5

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

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
6
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies....
1
by: Tom Jones | last post by:
Hi, I am unsure as to what the proper usage model for HttpWebRequest is. Do I create a new request instance each time I interact with a specific server? For example, if I need to perform a...
11
by: Keith Patrick | last post by:
Could someone explain to me the relationship between these two classes? I am ripping my hair out trying to divert an HttpRequest to a new location via an HttpWebRequest, but I cannot get my...
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...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
6
by: James MA | last post by:
I'm now writing a small program to communicate a web server to simulate a web client. I use te httpwebrequest to talk with the server, and it works find for "POST" method, however, when i test...
5
by: rlueneberg | last post by:
I am totally confused. Can someone please illuminate what is going on under the hood in this piece of code from John Lewis. My main confusion is how the cookieContainer can be passed to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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...

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.