title: Powershell WebClient gives up on encountering SNI error
body: <p>Has anyone found a way to either force a Powershell TLS client to not use SNI, or to continue with a connection past an SNI error from the server?</p>
  
  <p>I'm trying to make a powershell script download a big list of networks from our DHCP server, and format it for import into another system.</p>
  
  <p>I can download the data with a browser - watching the connection with Wireshark, I see the browser start the TLS handshake, send an SNI indication, the server respond with a "TLS Alert (Level: Warning, Description: Unrecognized Name)", the browser continue with the TLS connection unfazed, and the download succeeds.</p>
  
  <p>To debug the requests, I ran the connection through a local proxy (burp suite, which is a Java app), so I could see both the encrypted tunnel, and the content of the HTTP request.  This caused the connection to fail and the proxy to immediately return an error.  Apparently Java TLS code is unwilling to continue to connect past the TLS error that browsers take in stride.  If I force the proxy not to include Server Name Identification (SNI) in the request, it works (see the commented-out debug code below).</p>
  
  <p>Finally, removing the proxy from the mix, it seems the network code Powershell uses is also unwilling to continue past the error - it sends the same SNI indicator, receives the same TLS error, and then just hangs - no more packets are exchanged until 100 seconds have passed, then the connection is closed with a FIN packet, and Powershells returns a timeout error:</p>
  
  <p><code>Exception calling "DownloadFile" with "2" argument(s): "The operation has timed out"</code></p>
  
  <p>The code I'm using is this:</p>
  
  <pre><code>$webclient = new-object System.Net.WebClient
  
  #DEBUG for now - send requests through proxy
  
  #$proxy = new-object System.Net.WebProxy
  #$proxy.Address = "http://localhost:8080"
  #$webclient.proxy = $proxy
  
  # have to do this to ignore invalid certs from proxy
  #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
  
  #Also have to force the JRE running the proxy to not send SNI in the request - invoke as
  #java -Djsse.enableSNIExtension=false -jar c:\Users\MarkS\Burp\burpsuite_pro_v1.6.09.jar
  
  #end DEBUG
  
  $server = $(
      $input = Read-Host "server to connect to [our-DHCP-server-name]"
      if($input) {$input} else {"our-DHCP-server-name"}
  )
  
  $current_id = [Environment]::Username
  $user_id = $(
      $input = Read-Host "user ID to connect as [$current_id]"
      if($input) {$input} else {$current_id}
  )
  
  $password = Read-Host -AsSecureString 'password to connect with'
  
  $pscreds = new-object System.Management.Automation.PSCredential($user_id,$password)
  $webclient.Credentials = new-object System.Net.NetworkCredential($user_id, $pscreds.GetNetworkCredential().Password)
  
  #connect...
  
  $tempfile = [System.IO.Path]::GetTempFileName()
  $tempfile
  
  #...and download
  #NOTE - don't use XML - server returns badly malformed XML
  $url = "https://$server/wapi/v1.2.1/network?_max_results=10000000&amp;_return_type=json-pretty&amp;_return_fields%2B=extattrs"
  
  
  $webclient.DownloadFile($url, $tempfile)
  </code></pre>
  
  <p>Thanks very much!</p>