title: How to get Location header with WebClient after POST request
body: <p>I need to use <code>WebClient</code> to attain url that is inside response header, after succesfull POST request.</p>
  
  <p>However the headers I get after executing <code>WebClient.UploadValues</code> is the one from page <strong>after</strong> being redirected.</p>
  
  <p>Using browser (red to highlight the header I need):
  <a href="https://i.stack.imgur.com/hHkJl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hHkJl.png" alt="enter image description here"></a></p>
  
  <p>And this code that simulates same actions as in Browser with <code>WebClient</code>:</p>
  
  <pre><code>NameValueCollection formData = new NameValueCollection();
  formData["username"] = user;
  formData["password"] = password;
  
  byte[] responseBytes = webClient.UploadValues(loginUrl, "POST", formData);
  string response = Encoding.UTF8.GetString(responseBytes);
  
  string allheaders = "";
  for (int i = 0; i &lt; webClient.ResponseHeaders.Count; i++)
  {
      allheaders += Environment.NewLine + webClient.ResponseHeaders.GetKey(i) + " = " + webClient.ResponseHeaders.Get(i);                        
  }
  File.WriteAllText("headers_2.txt", "[HEADER_2] " + allheaders);
  </code></pre>
  
  <p>...gives as a result headers_2.txt that doesn't contain Location header (these headers are from the page that user is redirected to):</p>
  
  <blockquote>
    <p>[HEADER_2] 
    Pragma = no-cache
    Cache-Control = no-store, no-cache, must-revalidate
    Date = Wed, 14 Feb 2018 10:58:10 GMT
    Expires = Thu, 19 Nov 1981 08:52:00 GMT
    P3P = fffff
    Set-Cookie = sid=ffffff; path=/,gameapi_console=0; expires=Sat, 17-Mar-2018 10:58:10 GMT; Max-Age=2678400; path=/,bptid=ffffff; path=/
    Server = Apache
    Vary = Accept-Encoding,User-Agent
    Access-Control-Allow-Origin = *
    Content-Type = text/html; charset=UTF-8
    Transfer-Encoding = chunked</p>
  </blockquote>
  
  <p><strong>How to get Location header with WebClient? If I can whatsoever.</strong></p>
  
  <h2>UPDATE:</h2>
  
  <p>Thanks for comments.</p>
  
  <p>I forgot to show what I tried.
  So I created my own class that inherits from <code>WebClient</code>. I was thinking that if I read the Headers just in the moment when <code>UploadValuesCompleted</code> is fired - I will have the headers before being automatically redirected. Unfortunately for unknown reason the event is never fired even after I do call <code>webClient.UploadValues(loginUrl, "POST", formData)</code>.</p>
  
  <pre><code>class FAWebClient : WebClient
  {
      public event Action&lt;string&gt; OnLocationHeaderFound;
  
      public FAWebClient(Action&lt;string&gt; pOnLocationHeaderFound = null) :base()
      {
          UploadValuesCompleted += OnUploadValuesCompleted;
  
          if(pOnLocationHeaderFound != null)
              this.OnLocationHeaderFound += pOnLocationHeaderFound;
      }
  
      protected override void OnUploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) 
      {
          if (this.OnLocationHeaderFound != null)
          {
              for (int i = 0; i &lt; this.ResponseHeaders.Count; i++)
              {
                  System.Diagnostics.Debug.WriteLine(this.ResponseHeaders.GetKey(i) + " = " + this.ResponseHeaders.Get(i));
  
                  if (this.ResponseHeaders.GetKey(i).ToLower() == "location")
                  {
                      OnLocationHeaderFound(this.ResponseHeaders.Get(i));
                  }
              }
          }
  
          base.OnUploadValuesCompleted(e);
      }
  }
  </code></pre>
  
  <p>Also please be aware I have workaround that problem using <code>HttpClient</code>, simply setitng <code>HttpClient.AllowAutoRedirect</code> to false:</p>
  
  <pre><code> var handler = new HttpClientHandler()
   {
        AllowAutoRedirect = false
   };
  </code></pre>
  
  <p>Due to that I had to change a lot in my app logic, but it works.
  However I keep this question open if anyone will give solution or answer how to achieve that with <code>WebClient</code>.</p>