diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs index 5fb748ae5908f..a34ee205d0e4b 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs @@ -460,11 +460,15 @@ private void WriteBytes(ReadOnlySpan bytes) private void WriteAsciiString(string s) { + Debug.Assert(Ascii.IsValid(s)); + _writeBuffer.EnsureAvailableSpace(s.Length); - int length = Encoding.ASCII.GetBytes(s, _writeBuffer.AvailableSpan); - Debug.Assert(length == s.Length); - Debug.Assert(Encoding.ASCII.GetString(_writeBuffer.AvailableSpan.Slice(0, length)) == s); - _writeBuffer.Commit(length); + + OperationStatus status = Ascii.FromUtf16(s, _writeBuffer.AvailableSpan, out int bytesWritten); + Debug.Assert(status == OperationStatus.Done); + Debug.Assert(bytesWritten == s.Length); + + _writeBuffer.Commit(s.Length); } private void WriteString(string s, Encoding? encoding)