SslStream Escaping UTF-8 URL Encoding Above %7F (Multi-byte)?

AnaCoda 0 Reputation points
2023-05-30T20:17:03.7633333+00:00

I am trying to send special characters via an SslStream and StreamWriter that makes a URL GET request like so:


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();

This code should send ÉR, but from Wireshark I can see that writer.FlushAsync is making this request: GET /echo/%25C9R HTTP/1.1

It seems like ssl.WriteAsync is escaping the % character for some reason? This only occurs for multi-byte UTF8, AKA values starting from %80. Why might this be happening and how can I prevent it from escaping the % sign?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,357 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 31,196 Reputation points Microsoft Vendor
    2023-05-31T08:50:29.02+00:00

    Hi @AnaCoda , Welcome to Microsoft Q&A.

    As suggested by viorel:

    The solution is var path = "/echo/%C3%89%52".

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. AnaCoda 0 Reputation points
    2023-06-01T16:57:52.9566667+00:00

    Update: This seems to be a YARP problem, which is also escaping %2F creating implications for encoded paths.

    See
    microsoft/reverse-proxy#1777
    microsoft/reverse-proxy#1419
    dotnet/aspnetcore#11544

    Are there any workarounds for this?

    0 comments No comments