title: How can I post user name and password from a Windows application to a web page to obtain user-specific content?
body: <p>I want to fetch data from a webpage using C# and .NET. But page requires log in. HTML code of login page is something like this (page posts data to itself):</p>
  
  <pre><code>&lt;form id="frm" action="./default.aspx" method="post"&gt;
  &lt;input id="userName" type="text"&gt;
  &lt;input id="password" type="password"&gt;
  &lt;input type="submit" value="Send"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I want to post relevant data from a windows application to this page. My C# code is as follows:</p>
  
  <pre><code>var baseAddress = new Uri("https://www.website.com");
  var cookieContainer = new CookieContainer();
  using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
  using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
  {
      var content = new FormUrlEncodedContent(new[]
      {
          new KeyValuePair&lt;string,string&gt;("userName","myUserName"),
          new KeyValuePair&lt;string, string&gt;("password","myPassword")
      });
      var result = await client.PostAsync("/default.aspx",content);
      var resultText = await result.Content.ReadAsStringAsync();
      Console.WriteLine(resultText);
  }
  </code></pre>
  
  <p>When I manually fill the form and click the Send button user-specific page comes. But when I post the same data using my Windows application HTML code of the same login page comes, not the user specific page.</p>
  
  <p>I am doing something wrong. But I couldn't figure out.</p>