Here is working example:
This class extends default .NET WebClient with ability to store cookies:
public class CookieAwareWebClient : WebClient
{
public void Login(string loginPageAddress, NameValueCollection loginData)
{
var parameters = new StringBuilder();
foreach (string key in loginData.Keys)
{
parameters.AppendFormat("{0}={1}&",
HttpUtility.UrlEncode(key),
HttpUtility.UrlEncode(loginData[key]));
}
parameters.Length -= 1;
var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var buffer = Encoding.ASCII.GetBytes(parameters.ToString());
request.ContentLength = buffer.Length;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(buffer, 0, buffer.Length);
}
var container = request.CookieContainer = new CookieContainer();
using (var response = request.GetResponse())
{
CookieContainer = container;
}
}
public CookieAwareWebClient(CookieContainer container)
{
CookieContainer = container;
}
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieContainer CookieContainer { get; private set; }
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}
You can use it like this:
protected CookieAwareWebClient GetAuthenticatedClient()
{
var client = new CookieAwareWebClient();
var loginData = new NameValueCollection
{
{ "Email", "[email protected]" },
{ "Password", "testPassword" }
};
client.Login("https://myurl.com/login", loginData);
return client;
}
and then navigate to some internal page using:
using (var client = GetAuthenticatedClient())
{
var html = client.DownloadString("https://myurl.com/internalPage");
//do your stuff with received HTML here
}
I'm successfully using that logic in my ASP.NET projects.