Can not get cookies from a web page.

S

Shashank

Hi all,
I am making a http request to a html page residing on a server through
a desktop application .
The page is actually an html page with some perl queries on the top.
For this page I have some cookies set on my local browser.
The content of the page is dependent on the cookie value
My problem is that when I make the request through a browser, I am
getting the right contents, but when I make request through my desktop
application the contents are same as required.
Is there any way to make the http request via the browser or anyway to
retrieve the cookies.
Please help me out.

Thanks in advance
 
N

Nicholas Paldino [.NET/C# MVP]

Shashank,

What you have to do is read the cookie collection from where the browser
keeps the cookies. For IE, you can call the WinInet library functions
through the P/Invoke layer. For other browsers, you will have to make a
call dependent on the browser architecture.

Once you have that, you can use the CookieContainer request on the
HttpWebRequest class to associate cookies with the request you are making.

You will probably want to make sure you set the UserAgent property as
well, just so the service thinks you are coming through a browser (it might
make some decisions on what to send back based on that, but without knowing
what the service does or who controls it, it is impossible to say).

Hope this helps.
 
P

Peter Bromberg [C# MVP]

Here's a short example of using the CookieContainer:


private CookieContainer m_CookieContainer = null;
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://yoursite.com/yourpage.aspx");
request.CookieContainer = this.m_CookieContainer;
request.Timeout = TimeOutTime;
request.UserAgent = "whateveryouwanthere";
request.Accept = "text/html";
request.Referer = "whatever httpreferer if necessary"
request.KeepAlive = true;
request.MediaType = "text/html;";

HttpWebResponse w = null;
w = (HttpWebResponse) request.GetResponse();

//..etc

--Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top