title: Programmatically logging into a site
body: <p>It might sound silly; but can we programmatically login into a site such as Linkedin by passing our user credentials (userid and password)? I am not talking about using OAuth or other mechanism.</p>
  
  <p><em><strong>Edit:</em></strong></p>
  
  <p>I am using the following code in Page_Load method of an .aspx web page to login into Linkedin but it's not working. I see the login page again:</p>
  
  <pre><code>  try {
        string url = "https://www.linkedin.com/uas/login";
        string userName = "SomeEmail@SomeDomain.Com";
        string password = "--------";
        string domain = "linkedin.com";
        string postData = "session_key=" + userName + "&amp;session_password=" + password;
        string userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
        string method = "POST";
        string contentType = "text/html; charset=UTF-8";
  
        HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
  
        http.UserAgent = userAgent;            
        http.Method = method;
        http.ContentType = contentType;            
        http.Credentials = new NetworkCredential(userName, password, domain);
  
        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
        http.ContentLength = dataBytes.Length;
  
        http.Timeout = -1;
        http.KeepAlive = true;
        http.CookieContainer = new CookieContainer();
  
        using (Stream postStream = http.GetRequestStream())
        {
           postStream.Write(dataBytes, 0, dataBytes.Length);
        }
  
        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
  
        if (httpResponse.Cookies != null)
        {
           foreach (Cookie cok in httpResponse.Cookies)
           {
              if (cok != null)
              {
                 Response.Write(String.Format("Name = {0}- Value = {1}- Path = {2}&lt;br/&gt;", cok.Name, cok.Value, cok.Path));
              }
           }
        }   
  
        Response.Write(httpResponse.Headers.ToString() + "&lt;br/&gt;");
        Response.Write(new StreamReader(httpResponse.GetResponseStream()).ReadToEnd());
    } catch(Exception ex) 
    { 
        Response.Write(ex.ToString());
    }
  </code></pre>