WebClient and Cookies

  • Thread starter Mike Markiewicz
  • Start date
M

Mike Markiewicz

I would like to use WebClient on a page that requires a login before it can
be viewed. I want to be able to log in with IE and run my application from
there.

Is there a way, using System.Net.WebClient or something else, to make my web
requests using IE's cookies? I've already tried manually adding what I
think is the right cookie data to the header like so:


WebClient wc = new WebClient();
WebHeaderCollection head = new WebHeaderCollection();

head.Add("Cookie", "CFID=1312433; CFTOKEN=48763040");
wc.Headers = head;

byte[] data = wc.DownloadData(/* URI removed */);


Maybe I'm doing something wrong here?

Thanks,
Mike
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

You are much better off using the HttpWebRequest class. This way, you
can use the CookieContainer and related classes to set the values on your
cookies, instead of having to construct it yourself.

As for getting the cookies in IE, you will have to use some P/Invoke
code for that. You can use the InternetGetCookie function in the WinInet
library to get the cookies for a particular URL. From there, you can assign
the cookies to the CookieCollection on the HttpWebRequest.

It would be easy to create something reusable as well.

Hope this helps.
 

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