Search code examples
c#encoder

System.Net.WebUtility.HtmlDecode does not appear to work correctly


this is the code in my client (winform c# desktop):

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" + System.Net.WebUtility.HtmlEncode(sb.ToString()) + "DATA_END");
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();
}

this in the code in my server app which is a UWP application in c#:

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(Encoding.UTF8.GetString(data, 0, data.Length));
        dataRead = buffer.Length;
    }                       
}
System.Diagnostics.Debug.WriteLine(System.Net.WebUtility.HtmlDecode(request.ToString()));

and this is that result:

GET /DATA_START<informedworker><request%20name="Customer"%20action="GET"%20verb="*"></request></informedworker>DATA_END HTTP/1.1
Host: 192.168.0.6
Connection: Keep-Alive

as you can see the '<' and '>' are converted OK but the spaces are still read as %20??


Solution

  • From what I see, the HtmlDecode method uses the HtmlEntities static class which has the list of entities to decode. But there is no reference to the space character. You may want to try calling Uri.UnescapeDataString() instead.

    :)

    https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx