Using WebClient or WebRequest to login to a website and access data
Update:
See my comment below.
Here's what I did and it works (credit).
Add this class first:
namespace System.Net
{
using System.Collections.Specialized;
using System.Linq;
using System.Text;
public class CookieAwareWebClient : WebClient
{
public void Login(string loginPageAddress, NameValueCollection loginData)
{
CookieContainer container;
var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var query = string.Join("&",
loginData.Cast<string>().Select(key => $"{key}={loginData[key]}"));
var buffer = Encoding.ASCII.GetBytes(query);
request.ContentLength = buffer.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
container = request.CookieContainer = new CookieContainer();
var response = request.GetResponse();
response.Close();
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;
}
}
}
Usage:
public static void Main()
{
var loginAddress = "www.mywebsite.com/login";
var loginData = new NameValueCollection
{
{ "username", "shimmy" },
{ "password", "mypassword" }
};
var client = new CookieAwareWebClient();
client.Login(loginAddress, loginData);
}
How do I log into a site with WebClient?
If the problem you are having is you can authenticate but you cant keep the authentication cookie here is a cookie aware version of WebClient.
private class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
public CookieContainer CookieContainer { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
EDIT:
The link you gave me uses forms authentication with HTTP POST, I don't have the time to walk though it but at least it gives you a start with Google.
WebClient accessing page with credentials
I suspect that the web page that you are trying to access uses Forms Authentication. This means that you will have to provide a valid authentication cookie if you want to be able to access protected resources. And in order to obtain a valid authentication cookie you will have to first authenticate yourself by sending a POST request to the LogOn page which emits the cookie. Once you retrieve the cookie you will be able to send it along on subsequent requests on protected resources. You should also note that out of the box WebClient
doesn't support cookies. For this reason you could write a custom cookie aware web client:
public class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
{
CookieContainer = 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;
}
}
Now you could use this client to fire off the 2 requests:
using (var client = new CookieAwareWebClient())
{
var values = new NameValueCollection
{
{ "username", "john" },
{ "password", "secret" },
};
client.UploadValues("http://domain.loc/logon.aspx", values);
// If the previous call succeeded we now have a valid authentication cookie
// so we could download the protected page
string result = client.DownloadString("http://domain.loc/testpage.aspx");
}
Obviously due to the ViewState crapiness of ASP.NET you might need to send a couple of other parameters along your logon request. Here's what you could do: authenticate in a web browser and look with FireBug the exact parameters and headers that need to be sent.
C# WebClient Log onto Website
This is posted on server if you try to login in browser:
org.apache.struts.taglib.html.TOKEN=81243ce1a02ff285745f7c25b86234a9&showLogin=true&upgrade=&username=username&password=password&submit=Log+in
Try adding those values as well, and figure out how TOKEN is generated.
EDIT: Check if cookies that page gives you are submited back too.
ANOTHER EDIT: Too see what is going on between server and browser (=Firefox) when you are making a request or posting data use LiveHttpHeaders addon.
Related Topics
"The Semaphore Timeout Period Has Expired" Error for Usb Connection
What Are the Naming Conventions in C#
Which Pid Listens on a Given Port in C#
Troubleshooting "Program Does Not Contain a Static 'Main' Method" When It Clearly Does...
How Should You Diagnose the Error Sehexception - External Component Has Thrown an Exception
Mapping Database Views to Ef 5.0 Code First W/Migrations
How to Convert String "07:35" (Hh:Mm) to Timespan
Making a Web Request to a Web Page Which Requires Windows Authentication
Why Does Binarywriter Prepend Gibberish to the Start of a Stream? How to Avoid It
Regex: Repeated Capturing Groups
Problems Overwriting (Re-Saving) Image When It Was Set as Image Source
Is a Reference Assignment Threadsafe
Working with Appdomain.Assemblyresolve Event
How to Programmatically Convert Vb6 Formatting Strings to .Net Formatting Strings