System.Net.WebClient の認証proxy経由対応（環境変数設定）


こんにちは。
Windows の .NET 系の```System.Net.WebClient```で、下記の認証proxyの環境変数設定を使うようにするために[^1] [^2]、PowerShell用に ```Get-NewWebClient``` を作ってみました。

[^1]: NuGet （および clist コマンド）はこの環境変数設定を使ってproxyを通ります。
[^2]: 参考：[認証Proxy経由でのChocolateyの利用](http://qiita.com/tsukamoto/items/099fe6375c3c34404b5b)

```ps1
>> Get-Content .\Get-NewWebClient.ps1
function Get-NewWebClient {
  param()
  $wc = new-object System.Net.WebClient
  $proxyaddress = [Environment]::GetEnvironmentVariable('HTTP_PROXY') -replace 'http://', ''
  if ($proxyaddress -nq $null) {
    $p = $proxyaddress.Split("@")
    $proxy = new-object System.Net.WebProxy($p[$p.Length-1])
    if ($p.Length -eq 2) {
      $creds = $p[0].Split(":")
      $proxy.credentials = new-object System.Net.NetworkCredential($creds[0],$creds[1])
    }
    $wc.proxy = $proxy
  }
  $wc
}
>> $env:HTTP_PROXY
http://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORTNUMBER>
```

###Chocolatey

これを使って、[Chocolatey](https://chocolatey.org/) をインストールする例は、

```ps1
>> Set-ExecutionPolicy RemoteSigned
>> set HTTP_PROXY=http://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORTNUMBER>
>> .\Get-NewWebClient
>> (Get-NewWebClient).DownloadString('https://chocolatey.org/install.ps1') > install.ps1
>> set ChocolateyInstall=C:\ProgramData\chocolatey
>> .\install.ps1
>> clist -lo
chocolatey  0.9.10.3
```

この install.ps1 内でも ```Get-NewWebClient``` を使うように修正。

```console
$ diff -u install.ps1.orig install.ps1
--- install.ps1.orig
+++ install.ps1
@@ -75,47 +75,10 @@
 function Get-Downloader {
 param (
   [string]$url
  )
 
-  $downloader = new-object System.Net.WebClient
-
-  $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
-  if ($defaultCreds -ne $null) {
-    $downloader.Credentials = $defaultCreds
-  }
-
-  # check if a proxy is required
-  $explicitProxy = $env:chocolateyProxyLocation
-  $explicitProxyUser = $env:chocolateyProxyUser
-  $explicitProxyPassword = $env:chocolateyProxyPassword
-  if ($explicitProxy -ne $null) {
-    # explicit proxy
-    $proxy = New-Object System.Net.WebProxy($explicitProxy, $true)
-    if ($explicitProxyPassword -ne $null) {
-      $passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force
-      $proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd)
-    }
-
-    Write-Debug "Using explicit proxy server '$explicitProxy'."
-    $downloader.Proxy = $proxy
-
-  } elseif (!$downloader.Proxy.IsBypassed($url))
-  {
-    # system proxy (pass through)
-    $creds = $defaultCreds
-    if ($creds -eq $null) {
-      Write-Debug "Default credentials were null. Attempting backup method"
-      $cred = get-credential
-      $creds = $cred.GetNetworkCredential();
-    }
-    
-    $proxyaddress = $downloader.Proxy.GetProxy($url).Authority
-    Write-Debug "Using system proxy server '$proxyaddress'."
-    $proxy = New-Object System.Net.WebProxy($proxyaddress)
-    $proxy.Credentials = $creds
-    $downloader.Proxy = $proxy
-  }  
-  
+  $downloader = Get-NewWebClient
+ 
   return $downloader
 }
```

---------
なお Chocolatey 0.9.9 以降では、下記の環境変数を読み取るそうです。

```ps1
>> $env:chocolateyProxyLocation = "<HOSTNAME>:<PORTNUMBER>"
>> $env:chocolateyProxyUser = "<USERNAME>"
>> $env:chocolateyProxyPassword = "<PASSWORD>"
```

---------
Chocolatey 0.9.9.9 以降では、明示的に設定するそうです。

```ps1
>> choco config set proxy <HOSTNAME>:<PORTNUMBER>
>> choco config set proxyUser <USERNAME>
>> choco config set proxyPassword <PASSWORD>
```

-----------------