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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.