Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling new type in PowerShell v2 - Cookie Aware WebClient

I am trying to compile a "cookie aware" version of the WebClient class - but I can't seem to get over some of the hurdles of using the Add-Type cmdlet added in PowerShell v2. Here is the code I am trying to compile:

PS C:\> $def = @"
public class CookieAwareWebClient : System.Net.WebClient
{
  private System.Net.CookieContainer m_container = new System.Net.CookieContainer();
  protected override System.Net.WebRequest GetWebRequest(System.Uri address)
  {
    System.Net.WebRequest request = base.GetWebRequest(address);
    if (request is System.Net.HttpWebRequest)
    {
      (request as System.Net.HttpWebRequest).CookieContainer = m_container;
    }
    return request;
  }
}
"@

PS C:\> Add-Type -TypeDefinition $def

It can't seem to find the CookieContainer type can't be found (though it is fully qualified...) - clearly I am blind on something.

Edit: Updated the sample code to be correct and copy-n-pasteable, thanks!

like image 746
Goyuix Avatar asked Nov 23 '10 22:11

Goyuix


1 Answers

The second reference to CookieContainer with the constructor expression is fully qualified. The first reference, when declaring the field m_container is not. Make both fully qualified so Powershell can find them

private System.Net.CookieContainer m_container = new System.Net.CookieContainer();
like image 63
JaredPar Avatar answered Nov 03 '22 12:11

JaredPar