Hi! I'm trying to do a very simple program with C# that opens a webpages, put a nick and a password in the form of that page and that "presses" the login button... I don't know what to search online that could help me.

7 Years
Discussion Span
Last Post by Momerath
string loginUri = "http://www.somesite.com/login";
string username = "username";
string password = "password";
string reqString = "username=" + username + "&password=" + password;

byte[] requestData = Encoding.UTF8.GetBytes (reqString);
CookieContainer cc = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create (loginUri);
request.Proxy = null;
request.CookieContainer = cc;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;

using (Stream s = request.GetRequestStream()) {
    s.Write (requestData, 0, requestData.Length);
using (var response = (HttpWebResponse) request.GetResponse()) {
    foreach (Cookie c in response.Cookies) {  // just something to do with the response
        Console.WriteLine (c.Name + " = " + c.Value);

You are now now logged in. As long as you assign cc to subsequent WebRequest objects, you'll be treated as an authenticated user.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.