lundi 13 avril 2015

How to use reflection to change HttpWebRequest headers in Powershell?

I need to create an HttpWebRequest with specific headers using Powershell (v2) that are restricted by .NET. I read another post where this was done in C#, but I am running into difficulty accomplishing the same goal in Powershell


Here is what I have so far based off of the link above:



$httpWebRequest = [System.Net.HttpWebRequest][System.Net.WebRequest]::Create($url)
[System.Reflection.BindingFlags]$bflags = [Reflection.BindingFlags]"NonPublic" -bor [Reflection.BindingFlags]"Instance" -bor [Reflection.BindingFlags]"InvokeMethod"
$httpWebRequest.Headers.GetType().InvokeMember("ChangeInternal", $bflags, $null, $httpWebRequest.Headers, @{"Host" = "www.example.com"})


Unfortunately, the error response from Powershell is the following:



Exception calling "InvokeMember" with "5" argument(s): "Method 'System.Net.WebHeaderCollection.ChangeInternal' not found."



How should I go about invoking "ChangeInternal"? Is the error I'm getting have anything to do with the wrong assembly version?






Aucun commentaire:

Enregistrer un commentaire