TLS1.2 Powershell HttpWebClient支持

时间:2018-03-09 21:03:59

标签: windows powershell ssl httpwebrequest tls1.2

我试图将文件上传到https端点,但我一直遇到:

Could not create SSL/TLS secure channel.

搜索时,端点确实使用TLS 1.2,但在脚本中设置它似乎根本没有任何效果。有什么建议?完整的脚本是:

#[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$uri = New-Object "System.Uri" "https://.../docs"
$request = [System.Net.HttpWebRequest]::Create($uri)
  $request.Accept = "text/plain"
  $request.UserAgent = "foo/2.3.0.0 (windows; x86_64)"
  $request.ContentType = "application/x-tar"
  $request.Headers.Add("Content-Encoding","gzip");
  $request.Credentials = new-object System.Net.NetworkCredential("username","password","");

Try {
  $request.Method = "PUT"
  $requestStream = $request.GetRequestStream()
  $fileStream = [System.IO.File]::OpenRead("R:\\...-docs.tar.gz")
  $bufSize=10000
  $chunk = New-Object byte[] $bufSize
  while( $bytesRead = $fileStream.Read($chunk,0,$bufsize) )
  {
    $requestStream.write($chunk, 0, $bytesRead)
    $requestStream.Flush()
  }

  $responseStream = $request.getresponse()
  Write-Host "200";
  Write-Host (-join [System.Text.Encoding]::UTF8.GetChars($bodyBytes));

} Catch [System.Net.WebException] {
  $exception = $_.Exception;
  If ($exception.Status -eq [System.Net.WebExceptionStatus]::ProtocolError) {
    $response = $exception.Response -as [System.Net.HttpWebResponse];
    $reader = new-object System.IO.StreamReader($response.GetResponseStream());
    Write-Host ($response.StatusCode -as [int]);
    Write-Host $reader.ReadToEnd();
  } Else {
    Write-Host $exception;
  }
} Catch {
  Write-Host $_.Exception;
} finally {
  $fileStream.Close()
  $requestStream.Close()
  $responseStream.Close()

}

1 个答案:

答案 0 :(得分:1)

如果您的凭据不正确而不是401未经授权的响应,则会出现Could not create SSL/TLS secure channel.错误:(