title: How can i login to linkedin using HTTP POST Request with C#
body: <p>I am working on a simple Windows Forms program that take a username and password from a "Textbox" then it show my linked-in name in a "Messagebox".</p>
  
  <p>I want to accomplish the code with the using of "HttpWebRequest" or using any method to send my POST request to Linked-in then i can get the response and find my name to shown it in a "Messagebox".</p>
  
  <p>I am familiar with creating a "GET" Request and also i made some "POST" Requests but in this case i didn't know how can i send my "txt_UserName.Text" and "txt_Password" with the POST Request and how can i receives the Response.</p>
  
  <p>I tried to using Fiddler to capture POST request (=POST) from linkedin when i try to login but it captures more than 4 requests when i see the header of them it seem like a GET Request this is an example of one:</p>
  
  <pre><code>GET /nhome/?trk= HTTP/1.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  </code></pre>
  
  <p>and all of them have a multiple cookies values.</p>
  
  <p>This is my POST request code:</p>
  
  <pre><code>    public void SubmitData()
      {
          try
          {              
              string postData = "This is a test that posts this string to a Web server.";
              byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  
              // Create a request using a URL that can receive a post. 
              WebRequest request = WebRequest.Create("http://www.linkedin.com");
              // Set the Method property of the request to POST.
              request.Method = "POST";
              // Set the ContentLength property of the WebRequest.
              request.ContentLength = byteArray.Length;
              //Content Length
              request.ContentLength = byteArray.Length;
  
              // Get the request stream.
              Stream dataStream = request.GetRequestStream();
              // Write the data to the request stream.
              dataStream.Write(byteArray, 0, byteArray.Length);
              // Close the Stream object.
              dataStream.Close();
  
              // Get the response.
              WebResponse response = request.GetResponse();
              dataStream = response.GetResponseStream();
  
              StreamReader sr = new StreamReader(dataStream);
              MessageBox.Show(sr.ReadToEnd());
  
              sr.Close();
              dataStream.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error: " + ex.Message);
          }
      }
  </code></pre>
  
  <p>Now the only thing i wish to know, how can i send my username and password as a values to login to linked-in?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>Below is my second try, it's ok, i can now send the User and Password in postData and i can store the Cookies and retrive it. but there are two issues:
  1- how can i make sure that the login is accomplished and not failed
  2- if the login is accomplished i want to know what is the second step to get my name from profile, is it made another request or what ? </p>
  
  <pre><code>  private void button1_Click(object sender, EventArgs e)
      {
          PostMessage();
      }
  
  
      private void PostMessage()
      {
          try { 
  
          // POST Data and the POST uri
          string    postData = "isJsEnabled=true&amp;source_app=&amp;session_key=" + textBox1.Text + "&amp;session_password=" + textBox2.Text + "&amp;signin=Sign+In&amp;session_redirect=";
          string    uri = "https://www.linkedin.com/uas/login-submit";
  
          // Encoding the POST Data
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  
          // Create the POST Request
          HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(uri);
  
          //POST Parameters (Method and etc.)
          WebReq.Method = "POST";
          WebReq.ContentType = "application/x-www-form-urlencoded";
          WebReq.ContentLength = byteArray.Length;
  
          // Set the POST Request Cookies
          var cookieContainer = new CookieContainer();
          WebReq.CookieContainer = cookieContainer;
  
          // Get the request stream.
          Stream dataStream = WebReq.GetRequestStream();
          // Write the data to the request stream.
          dataStream.Write(byteArray, 0, byteArray.Length);
          // Close the Stream object.
          dataStream.Close();
  
  
          // Get the response.
          HttpWebResponse response = (HttpWebResponse)WebReq.GetResponse();
          dataStream = response.GetResponseStream();
  
          StreamReader sr = new StreamReader(dataStream);
          //MessageBox.Show(sr.ReadToEnd());
          sr.Close();
          dataStream.Close();
  
          if (response.StatusCode != HttpStatusCode.OK)
          {
              MessageBox.Show(" Error: " + response.StatusDescription);
              response.Close();
          }
  
          foreach (Cookie cook in response.Cookies)
          {
              MessageBox.Show(cook.Name + " " + cook.Value);
          }
  
              }
  
          catch (Exception ex)
          {
              MessageBox.Show("POST Message Error: " + ex.Message);
          }
  
      }
  </code></pre>