在 Web 响应中将 < 和 > 等字符读回 ascii

Reading charcaters like < and > back as ascii in web response

我已经为 Raspberry Pi(Windows 物联网)创建了自己的网络服务器。

我有 winform 桌面应用程序向我的网络服务器发出请求:

StringBuilder sb = new StringBuilder();
sb.Append("<informedworker>");
sb.Append("<request name='Customer' action='GET' verb='*'>");                
sb.Append("</request>");
sb.Append("</informedworker>");

Uri url = new Uri("http://192.168.0.6/DATA_START" + sb.ToString() + "DATA_END");//, query, key));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    HttpStatusCode statusCode = ((HttpWebResponse)response).StatusCode;
    string contents = reader.ReadToEnd();
}

在我的网络服务器 (UWP) 中,我有:

StringBuilder request = new StringBuilder();
using (IInputStream input = args.Socket.InputStream)
{
 byte[] data = new byte[BufferSize];
 IBuffer buffer = data.AsBuffer();
 uint dataRead = BufferSize;
 while (dataRead == BufferSize)
 {
     await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
     request.Append(ASCIIEncoding.ASCII.GetString(data, 0, data.Length));
     dataRead = buffer.Length;
 }
 System.Diagnostics.Debug.WriteLine(request.ToString());

我在这里读到的回复是:

GET /DATA_START%3Cinformedworker%3E%3Crequest%20name='Customer'%20action='GET'%20verb='*'%3E%3C/request%3E%3C/informedworker%3EDATA_END HTTP/1.1
Host: 192.168.0.6
Connection: Keep-Alive

如您所见,%3E%3C 已替换“<”。

我当然可以创建自己的解析器来 'translate' 这些 'special' 个字符。

是否有针对这种情况的内置编码器?

谢谢

看看 System.Net.WebUtility 中的 UrlDecode