我尝试通过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在浏览器中运行正常。我想知道是什么导致了这种行为。
1条答案
按热度按时间hrirmatl1#
更新:这似乎是一个YARP问题,这也是转义%2F,对编码路径产生影响。
参见https://github.com/microsoft/reverse-proxy/issues/1777
https://github.com/microsoft/reverse-proxy/issues/1419
https://github.com/dotnet/aspnetcore/issues/11544