title: How to use reflection to change HttpWebRequest headers in Powershell?
body: <p>I need to create an HttpWebRequest with specific headers using Powershell (v2) that are restricted by .NET. I read another <a href="https://stackoverflow.com/a/7560279">post</a> where this was done in C#, but I am running into difficulty accomplishing the same goal in Powershell</p>
  <p>Here is what I have so far based off of the link above:</p>
  <pre><code>$httpWebRequest = [System.Net.HttpWebRequest][System.Net.WebRequest]::Create($url)
  [System.Reflection.BindingFlags]$bflags = [Reflection.BindingFlags]&quot;NonPublic&quot; -bor [Reflection.BindingFlags]&quot;Instance&quot; -bor [Reflection.BindingFlags]&quot;InvokeMethod&quot;
  $httpWebRequest.Headers.GetType().InvokeMember(&quot;ChangeInternal&quot;, $bflags, $null, $httpWebRequest.Headers, @{&quot;Host&quot; = &quot;www.example.com&quot;})
  </code></pre>
  <p>Unfortunately, the error response from Powershell is the following:</p>
  <blockquote>
  <p>Exception calling &quot;InvokeMember&quot; with &quot;5&quot; argument(s): &quot;Method 'System.Net.WebHeaderCollection.ChangeInternal' not found.&quot;</p>
  </blockquote>
  <p>How should I go about invoking &quot;ChangeInternal&quot;? Is the error I'm getting have anything to do with the wrong assembly version?</p>