.net SslStream正在转义高于%7F的UTF-8 URL编码(多字节)?

lmvvr0a8  于 2023-06-07  发布在  .NET
关注(0)|答案(1)|浏览(301)

我尝试通过SslStream和StreamWriter发送特殊字符,它发出URL GET请求,如下所示:

var req = new HttpRequestMessage( HttpMethod.Get, m_client.BaseAddress );
m_sessionClient.AddSessionInfoTo( req );

var uri = req.RequestUri;

var client = new TcpClient( uri.Host, uri.Port );

Stream stream = client.GetStream();

var ssl = new SslStream( stream );

ssl.AuthenticateAsClient( uri.Host );


var writer = new StreamWriter(
                ssl,
                new UTF8Encoding( encoderShouldEmitUTF8Identifier: false )
);

await writer.WriteAsync( $"{req.Method.Method} " );

await writer.FlushAsync();

var path = "/echo/%C9%52";
var rawPathAndQuery = Encoding.UTF8.GetBytes( path );
await ssl.WriteAsync( rawPathAndQuery, 0, rawPathAndQuery.Length );


await writer.WriteAsync( " HTTP/1.1\r\nHost: " );

await writer.WriteAsync( req.RequestUri.Host );

await writer.WriteAsync( "\r\n" );

await writer.WriteAsync( "\r\n" );

await writer.FlushAsync();

这段代码应该发送ÉR,但是从Wireshark我可以看到那个writer。FlushAsync发出了这个请求:GET /echo/%25C9R HTTP/1.1
似乎ssl.WriteAsync因为某种原因转义了%字符?这仅适用于从%80开始的多字节UTF8,即值。为什么会发生这种情况,我如何防止它脱离%符号?
我以为请求是ÉR,而不是%25C9R
我还想澄清的是,我正在寻找一个解释,而不一定是一个修复。我在StreamWriter或SslStream中找不到任何说明UTF8转义的文档,除了访问
echo/%C9在浏览器中运行正常。我想知道是什么导致了这种行为。

相关问题