PowerShell: System.Net.Http.HttpClient always fails on first attempted connection

Prerequisites

Steps to reproduce

This is a re-report of Issue #19486, which was resolved for inactivity but still persists in 7.4.0. I have golfed the problem down to the following minimum code example.

> PS> $client = New-Object -TypeName System.Net.Http.HttpClient
> PS> $r1=$client.GetStringAsync('http://www.google.com')
> PS> $r1.Status
> Faulted
> PS> $r1.Exception.InnerException.InnerException.Message
> Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
> PS> $r1.Exception.InnerException.InnerException.InnerException | Select SocketErrorCode,ErrorCode
> SocketErrorCode ErrorCode
> ConnectionReset     10054
> PS> $r2=$client.GetStringAsync('http://www.google.com')
> PS> $r2.Status
> RanToCompletion

Expected behavior

Should connect the first time.

Actual behavior

Always fails on the first attempt to connect to a site.

Error details

No output.

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

PowerShell 5.1 .NET Framework 4.8
PowerShell 7.0 netcoreapp3.1
PowerShell 7.1 net5.0
PowerShell 7.2 net6.0
PowerShell 7.3 net7.0
PowerShell 7.4 net8.0

actually under mono in the kali WSL2

Alas that doesn’t really help PowerShell based on .NET Core and just adds 3 additional layers of confusion.

I suggest download the .NET SDK 8.0 on Windows and demonstrate the problem on Windows side by side with it working in PowerShell 5.1.

You say it stopped working on PowerShell 7.2, that used .NET SDK 6.0

Are you also able to demonstrate it on Windows using curl with full tracing on? ( and I do mean real curl.exe, not the PowerShell 5 alias)