473,698 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting Cookies into HttpWebRequests

I've spent half a day on this with no success, so I'm hoping someone
can point out what I'm doing wrong.

I have an ASP.NET Server-side component that I've created, and as part
of its Render function, it requests another webpage (which the user
defines) through an HttpWebRequest object.
This page that is being called requires the cookie objects that are
sent to the page hosting the control. So, as shown in the code below,
I'm looping through the host page's HttpContext object's Cookies (which
are instances of System.Web.Http Cookie) and creating instances of
System.Net.Cook ie. These are then added to a
System.Net.Cook ieCollection, which is tied to the HttpWebRequest object.

Here's the code, I'll explain the problem below it.

HttpWebRequest request =
(HttpWebRequest )WebRequest.Cre ate(sTreePage+" ?load=initial") ;
CookieContainer cookies = new CookieContainer ();
for (int i = 0; i < context.Request .Cookies.Count; i++) {
System.Web.Http Cookie hc = context.Request .Cookies[i];
Cookie c = new Cookie(hc.Name, hc.Value);
c.Domain = "localhost" ;
cookies.Add(c);
}
request.CookieC ontainer = cookies;
HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();

The code above runs fine, until it gets to the final line, where it
(eventually) times out on the GetResponse().
If I leave out the cookies section (ie. just use the first and last
lines) it works fine. But I need the data the cookies hold on the next
page. And, If other people are using the control, I'd like this layer
to be as transparent as possible.

The main annoyance is that there's two different, incompatible (afaik)
Cookie classes, so I can't just copy them between collections.

I've tried CookieContainer .Add(Uri, Cookie), using the RequestUri from
the HttpWebRequest object - which only works if the cookie domain is
".local" - but its no different. I have to set the domain, else it
throws an exception. And yes, I'm running this all off my local machine.

So how is what I'm doing wrong? Why isn't this working?
Any suggestions or comments are extremely welcome.

Thanks

Pat Allan
=============== =========
Solutions Developer
Managed Business Outcomes
Melbourne, Australia
Nov 15 '05 #1
0 1452

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

Similar topics

3
1536
by: James | last post by:
I am in the process of running my site through WC3 validation. I assume the validator at www.w3c.org cant accept cookies and so PHP is attaching &PHPSESSID=lasklijasj09jsad (or similar) to my URLS. The problem is the leading character is a literal & rather than a &amp; and this is stopping the validator telling me my pages are valid. What do I do here --
4
3877
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any new site I goto seems to prompt me to store their cookie. The other is Pocket IE on Pocket PC 2002, with the cookies set to 'enabled'. My problem is that the cookies dont seem to be being written with my ASP. I dont get the prompt to store...
20
3544
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
6
3045
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a cookie (say Response.Cookies ("TEST")) and you have a query string variable &test=x or &Test=x and you get Request.QueryString to parse the query string, the cookie that gets dropped matches the case of the query string, not what your code says. ...
6
3497
by: Mike | last post by:
Hi, I've been using httpwebrequest / xmlserverhttp to log into a website via POST. I've received all the response headers back, some of which contain the "set-cookie" headers. When performing any other action on the website I need to submit the JSESSION id and BSESSIONID as part of my POSTS. I was just wondering where these session ids originate, from the server
7
3029
by: djc | last post by:
I see many ways to accomplish setting up proxy settings for a web request in the documentation but many of them are marked as obsolete. 1) What is the current and correct way to tell all my httpWebRequests within a class to *not* use any proxy server? I don't want to setup the proxy on each webRequest within my class on an individual basis, and I don't want to rely on the bypassLocal setting, or the host exclusion list since I don't...
7
2866
by: Dean | last post by:
This seems like it should be easy but I am stumped. I am trying take a variable, add to it, and insert the result as a new record into Access. The ending result is: INSERT INTO Period (CoID, CkDate) Values 1, #2007/01/26#)
6
1788
by: =?Utf-8?B?dGF6?= | last post by:
i am trying to use HttpWebRequest to get the HTML of a webpage on a site. problem is that the page is hidden behind a username-password login screen which sets a cookie when the user has successfully logged. so, how can i get the HTML of the required page via HttpWebRequest ? ive tried using CookieContainer and SetCookies but im not sure im using them properly. any help/tips appreciated. thanks.
2
1311
by: John Gordon | last post by:
I'm developing a web application that needs a semi-persistent way to store information. I've looked at some options such as writing entries to a database table or creating small textfiles, but I'm not thrilled with anything I've come up with so far. Then a coworker suggested that I could just insert a cookie into the current session, which would store whatever information I wanted. This seems like a really good solution. That's what...
0
8611
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
9031
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
8904
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
8876
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
7741
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...
1
6531
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
5867
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
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
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.