title: c# how do I loop through all headers of HttpRequestHeaders
body: <p>I want to loop through all headers of <code>HttpRequestHeaders</code> of <code>HttpRequestMessage</code>.</p>
  
  <p>I am just migrating my code from <code>HttpWebRequest</code> to <code>HttpClient</code>.
  With <code>HttpWebRequest</code> I used following code to loop through its <code>WebHeaderCollection</code>:</p>
  
  <pre><code>StringBuilder headers_ = new StringBuilder();
  foreach (string key in headers.AllKeys)
  {
      headers_.Append(key).Append(':').Append(headers[key]).Append(' ');
  }
  headers_.Length--;
  </code></pre>
  
  <p>but <code>HttpRequestHeaders</code> doesn't have AllKeys method. It only has <code>GetValues(string name)</code> which only retrieves one specified header.</p>
  
  <pre><code>HttpRequestMessage requestMessage = new HttpRequestMessage
  {
      Method = HttpMethod.Post,
      RequestUri = new Uri("https://postman-echo.com/post"),
      Headers = {
          { "header1", "aa" },
          { "header2", "bb" },
      },
      Content = new StringContent("hi=aa")
  }
  </code></pre>
  
  <p>How do I programmatically retrieve pairs of { "header1", "aa" }, { "header2", "bb" } from <code>requestMessage</code>?</p>