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

Extracting String Info from WebRequest Cookie


Dear C#dex,

I define a variable: HttpWebRequest webRequest and run the following
request

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;

The webRequest object returns values and in the debugger I can see the
value I want in the property

webRequest._ChallengedUri.AbsoluteUri;

However that property is protected and not available outside of the
debugger. Does anyone have a suggestion how I might obtain programmatic
access to the property or a similar public property that contains the
same info?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
17 7210
Hi James:

Are you looking for a cookie value? You can attach a CookieContainer
instance to your request and then iterate through the collection of
Cookie objects.

--
Scott
http://www.OdeToCode.com
On Thu, 09 Sep 2004 07:43:40 -0700, James Johnson
<jj******@hotmail.com> wrote:

Dear C#dex,

I define a variable: HttpWebRequest webRequest and run the following
request

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;

The webRequest object returns values and in the debugger I can see the
value I want in the property

webRequest._ChallengedUri.AbsoluteUri;

However that property is protected and not available outside of the
debugger. Does anyone have a suggestion how I might obtain programmatic
access to the property or a similar public property that contains the
same info?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #2
James Johnson wrote:
Dear C#dex,

I define a variable: HttpWebRequest webRequest and run the following
request

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;

The webRequest object returns values and in the debugger I can see the
value I want in the property

webRequest._ChallengedUri.AbsoluteUri;


Um... what exactly is in there you'd like to have as well?

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #3

Dear Joerg

The property
webRequest._ChallengedUri.AbsoluteUri;


contains a uri such as "http://localhost/FirstProject/WebForm4.aspx"

I am trying to figure out how to extract that string. I have tried to
get something in a cookie container

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;

// write the form values into the request message
StreamWriter requestWriter = new
StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();

// we don't need the contents of the response, just the cookie it
issues
webRequest.GetResponse().Close();

System.Net.CookieCollection myCookieColl =
webRequest.CookieContainer.GetCookies(siteUri);

I can get a value for a cookie from myCookieColl in the debugger, but it
is encoded (e.g. "t0m2qf55lelfzo55xsuxdi55") and there is no exposed
Item[] property for myCookieColl that would enable me to step through
the cookies, so I am stuck at present. I can't figure out how to get a
cookie out of the collection and I can't figure out how to decode the
Value.

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
James Johnson wrote:
Dear Joerg

The property
webRequest._ChallengedUri.AbsoluteUri;
contains a uri such as "http://localhost/FirstProject/WebForm4.aspx"

I am trying to figure out how to extract that string.


I just wonder why would want to extract low level implementation of the
HttpWebRequest -- what part of the request do you need to figure out that
you do not or can not control?
I have tried to
get something in a cookie container

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;

// write the form values into the request message
StreamWriter requestWriter = new
StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();

// we don't need the contents of the response, just the cookie it
issues
webRequest.GetResponse().Close();

System.Net.CookieCollection myCookieColl =
webRequest.CookieContainer.GetCookies(siteUri);

I can get a value for a cookie from myCookieColl in the debugger, but
it is encoded (e.g. "t0m2qf55lelfzo55xsuxdi55") and there is no
exposed Item[] property for myCookieColl that would enable me to step
through the cookies, so I am stuck at present. I can't figure out
how to get a cookie out of the collection and I can't figure out how
to decode the Value.


CookieCollection *does* have in indexer. See
http://msdn.microsoft.com/library/de...sitemtopic.asp

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #5
James Johnson wrote:
Dear Joerg

The property
webRequest._ChallengedUri.AbsoluteUri;
contains a uri such as "http://localhost/FirstProject/WebForm4.aspx"

I am trying to figure out how to extract that string.


I just wonder why would want to extract low level implementation of the
HttpWebRequest -- what part of the request do you need to figure out that
you do not or can not control?
I have tried to
get something in a cookie container

webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;

// write the form values into the request message
StreamWriter requestWriter = new
StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();

// we don't need the contents of the response, just the cookie it
issues
webRequest.GetResponse().Close();

System.Net.CookieCollection myCookieColl =
webRequest.CookieContainer.GetCookies(siteUri);

I can get a value for a cookie from myCookieColl in the debugger, but
it is encoded (e.g. "t0m2qf55lelfzo55xsuxdi55") and there is no
exposed Item[] property for myCookieColl that would enable me to step
through the cookies, so I am stuck at present. I can't figure out
how to get a cookie out of the collection and I can't figure out how
to decode the Value.


CookieCollection *does* have in indexer. See
http://msdn.microsoft.com/library/de...sitemtopic.asp

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #6

Well, I have discovered that all you have to do to reference a cookie in
a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];

However, I still face the problem of the cookie.Value being encoded. Is
it bytes? Is it encrypted? How do you deal with the value and decode
it?

Thanks,

James J.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

Well, I have discovered that all you have to do to reference a cookie in
a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];

However, I still face the problem of the cookie.Value being encoded. Is
it bytes? Is it encrypted? How do you deal with the value and decode
it?

Thanks,

James J.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8
James Johnson wrote:
Well, I have discovered that all you have to do to reference a cookie
in a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];
If you're coming from VB.NET: You don't use the Item property in C#.
Instead, you access an item within a collection using the [] operator (aka
indexer), similar to accessing array items.

In additon to accessing a cookie by index, you can also access it by name.

Cookie cookie = myCookieColl["DeliciousCookie"];
However, I still face the problem of the cookie.Value being encoded.
Is it bytes? Is it encrypted? How do you deal with the value and
decode it?


Cookie.Value delivers the value "as is". You have to check with the
server-side application developers how they encoded or encrypted the
value -- chances are, they're not going to tell you ;-)

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #9
James Johnson wrote:
Well, I have discovered that all you have to do to reference a cookie
in a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];
If you're coming from VB.NET: You don't use the Item property in C#.
Instead, you access an item within a collection using the [] operator (aka
indexer), similar to accessing array items.

In additon to accessing a cookie by index, you can also access it by name.

Cookie cookie = myCookieColl["DeliciousCookie"];
However, I still face the problem of the cookie.Value being encoded.
Is it bytes? Is it encrypted? How do you deal with the value and
decode it?


Cookie.Value delivers the value "as is". You have to check with the
server-side application developers how they encoded or encrypted the
value -- chances are, they're not going to tell you ;-)

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #10

How about it Scott? You are the guy that suggested that I don't need
the contents of the response, just the cookie it issues. How do I get
the Uri out of the cookie?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11

How about it Scott? You are the guy that suggested that I don't need
the contents of the response, just the cookie it issues. How do I get
the Uri out of the cookie?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12
Joerg Jooss wrote:
James Johnson wrote:
Well, I have discovered that all you have to do to reference a cookie
in a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];


If you're coming from VB.NET: You don't use the Item property in C#.
Instead, you access an item within a collection using the [] operator
(aka indexer), similar to accessing array items.

In additon to accessing a cookie by index, you can also access it by
name.
Cookie cookie = myCookieColl["DeliciousCookie"];
However, I still face the problem of the cookie.Value being encoded.
Is it bytes? Is it encrypted? How do you deal with the value and
decode it?


Cookie.Value delivers the value "as is". You have to check with the
server-side application developers how they encoded or encrypted the
value -- chances are, they're not going to tell you ;-)


OK, I just read the entire thread again because I somehow missed James'
original posting, but I still don't get why should have
webRequest._ChallengedUri.AbsoluteUri anything to do with your cookie's
value? If you want to get retrieve local cookies, call

CookieCollection cookies = CookieContainer.GetCookies(new
Uri("http://localhost/"));

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #13
Joerg Jooss wrote:
James Johnson wrote:
Well, I have discovered that all you have to do to reference a cookie
in a cookie collection is to provide an index directly to the cookie
collection such as:

Cookie cookie = myCookieColl[0];


If you're coming from VB.NET: You don't use the Item property in C#.
Instead, you access an item within a collection using the [] operator
(aka indexer), similar to accessing array items.

In additon to accessing a cookie by index, you can also access it by
name.
Cookie cookie = myCookieColl["DeliciousCookie"];
However, I still face the problem of the cookie.Value being encoded.
Is it bytes? Is it encrypted? How do you deal with the value and
decode it?


Cookie.Value delivers the value "as is". You have to check with the
server-side application developers how they encoded or encrypted the
value -- chances are, they're not going to tell you ;-)


OK, I just read the entire thread again because I somehow missed James'
original posting, but I still don't get why should have
webRequest._ChallengedUri.AbsoluteUri anything to do with your cookie's
value? If you want to get retrieve local cookies, call

CookieCollection cookies = CookieContainer.GetCookies(new
Uri("http://localhost/"));

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #14
Hi James:

I was assuming (assumptions are bad I know) from the title of the post
that the value you needed was ultimately stored in the cookie. If the
server side developers decided the cookie contents need to be
encrypted, than as Joerg pointed out you'd have to ask them about it.

I wasn't quite sure why you were trying to peek at
_ChallengedUri.AbsoluteUri, but I'm guessing now that is because you
need to know some URL, and that URL is not the TARGET_URL used when
you create the WebRequest. If the WebRequest is bouncing around
between URLs, you can set the AllowAutoRedirect property of
HttpWebRequest to false and parse the redirects yourself.

If that's not what you are looking for, perhaps you could try to
rephrase the question? I guess I'm not entirely sure what you are
looking for.

Apologies for the confusion,

--
Scott
http://www.OdeToCode.com

On Fri, 10 Sep 2004 07:12:09 -0700, James Johnson
<jj******@hotmail.com> wrote:

How about it Scott? You are the guy that suggested that I don't need
the contents of the response, just the cookie it issues. How do I get
the Uri out of the cookie?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #15
Hi James:

I was assuming (assumptions are bad I know) from the title of the post
that the value you needed was ultimately stored in the cookie. If the
server side developers decided the cookie contents need to be
encrypted, than as Joerg pointed out you'd have to ask them about it.

I wasn't quite sure why you were trying to peek at
_ChallengedUri.AbsoluteUri, but I'm guessing now that is because you
need to know some URL, and that URL is not the TARGET_URL used when
you create the WebRequest. If the WebRequest is bouncing around
between URLs, you can set the AllowAutoRedirect property of
HttpWebRequest to false and parse the redirects yourself.

If that's not what you are looking for, perhaps you could try to
rephrase the question? I guess I'm not entirely sure what you are
looking for.

Apologies for the confusion,

--
Scott
http://www.OdeToCode.com

On Fri, 10 Sep 2004 07:12:09 -0700, James Johnson
<jj******@hotmail.com> wrote:

How about it Scott? You are the guy that suggested that I don't need
the contents of the response, just the cookie it issues. How do I get
the Uri out of the cookie?

Thanks,

James J.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #16
Dear Scott,

Here is a quote from your code in the article you referenced.

// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();

// now we can send out cookie along with a request for the protected
page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
responseReader = new
StreamReader(webRequest.GetResponse().GetResponseS tream());

Maybe I am making an erroneous assumption here, but I assumed that
SECRET_PAGE_URL came from the cookie. Otherwise I don't know how you
get the new URL. Also I can see the response URI in the webRequest
object in the _ChallengedUri.AbsoluteUri property when I use the
debugger. That URI is the URI that I want. However, as I have
mentioned, I can't get it programmatically.

Thanks for your help,

James J.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #17
Hi James:

I think I might have picked a bad variable name.

In my article I knew both URLs I needed beforehand.

LOGIN_URL represents a login page with username / password textbox
controls for forms authentication.

SECRET_PAGE_URL represents a page protected by forms authentication. I
need to login before I can request the secret page succesfully. If I
don't log in the server won't let me see the page.

First I request LOGIN_URL first to get the viewstate values ASP.NET
expects me to post back. You would not nessecarily need to do this for
all sites.

Next I POST to the LOGIN_URL to simulate a user logging in with a web
browser, that is a username and password are sent as POST data for the
web application to parse out and verify. If succesful, most forms
authentication schemes will send a cookie down in the response
headers. You can think of this cookie as a ticket. The ticket gives
you access to protected pages on the site, but in order to get the
protedted pages you have to present the ticket to the server. There is
really nothing I had to get out of the cookie, I just need the cookie
so I can prove I authenticated.

In order to reach the SECRET_PAGE_URL then, which is nothing more than
some page protected by forms authentication, I send the cookie value
along with the request. The server code sees this cookie and realizes
I have succesfully logged in previously, so it responds with the HTML
for the secret page.

Am I making any sense?

--
Scott
http://www.OdeToCode.com

On Fri, 10 Sep 2004 10:54:34 -0700, James Johnson
<jj******@hotmail.com> wrote:
Dear Scott,

Here is a quote from your code in the article you referenced.

// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();

// now we can send out cookie along with a request for the protected
page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
responseReader = new
StreamReader(webRequest.GetResponse().GetResponse Stream());

Maybe I am making an erroneous assumption here, but I assumed that
SECRET_PAGE_URL came from the cookie. Otherwise I don't know how you
get the new URL. Also I can see the response URI in the webRequest
object in the _ChallengedUri.AbsoluteUri property when I use the
debugger. That URI is the URI that I want. However, as I have
mentioned, I can't get it programmatically.

Thanks for your help,

James J.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #18

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

Similar topics

0
by: | last post by:
Hello, I can use WebRequest when accessing web sites that don't require a cookie. However, when I want to use it to get an html page from a web site that I have a current cookie for, I an...
1
by: Peter | last post by:
Hi I am trying to do very simple http client that requires a login, copy cookie snet by a server and does a request with the cookies. WebRequest->CookieContainer/WebResponse->Cookies does not...
2
by: Andres | last post by:
I am creating a web request (HttpWebRequest) from a web page in order to retrieve the Html from another page and embed it in the calling page. When you create a HttpWebRequest the request is...
3
by: news | last post by:
I am trying to get at the source of a web page. Looking at the innerHTML element is only part of the story. In IE, right-clicking on various different parts of the page gives me different results...
0
by: dmbkiwi | last post by:
I am trying to extract the value of a cookie from a CookieJar() object using cookielib. I have a CookieJar() object called cj. Printing cj gives: <_LWPCookieJar.LWPCookieJar> But i can't...
1
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie...
6
by: matt | last post by:
hello, i am having trouble doing something. when a user triggers a certain event in my app, i need to initiate another web request to one of my other webpages, programmatically. currently, i do...
0
by: Rama Jayapal | last post by:
i use the following code to obtain the html content of the page now with the help of this string strml in my code which has the html content i need to extract the tags like <input..> in them ...
1
by: dschu012 | last post by:
I am logging into a site using a WebBrowser. Once I log in I want to save an Image from within it without having to make the WebBrowser navigate to the URL where the Image is stored. Currently I am...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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...
0
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...

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.