【问题标题】:TLS1.2 Powershell HttpWebClient supportTLS1.2 Powershell HttpWebClient 支持
【发布时间】:2018-08-18 12:26:04
【问题描述】:

我正在尝试将文件上传到 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()

}

【问题讨论】:

  • 你能从异常中得到什么吗?您确定端点使用 TLS 1.2 而不是 1.1?
  • @briantist 不,例外是相当通用的,根据ssllabs.com/ssltest 它是 TLS 1.2 是的。
  • 您能否检查一下HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client 并查看是否由于某种原因禁用了 TLS 1.2?还有哪个操作系统?哪个版本的 PowerShell?
  • 嗯,我在 procols 下只有一个“SSL 2.0”条目。这是 Windows 10 版本 10.0.16299.248 和 powershell 5.1.16299.248
  • 我很抱歉我误解了你的意图......

标签: windows powershell ssl httpwebrequest tls1.2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2020-12-22
    • 1970-01-01
    • 2014-08-19
    • 2017-03-29
    • 2014-08-16
    相关资源
    最近更新 更多