这是我客户端的代码(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();
}
这在我的服务器应用程序中的代码中,这是c#中的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(Encoding.UTF8.GetString(data, 0, data.Length));
dataRead = buffer.Length;
}
}
System.Diagnostics.Debug.WriteLine(System.Net.WebUtility.HtmlDecode(request.ToString()));
这就是结果:
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
你可以看到&#39;&lt;&#39;和&#39;&gt;&#39;转换为OK但空格仍被读为%20 ??
答案 0 :(得分:1)
从我看到的,HtmlDecode方法使用HtmlEntities静态类,它具有要解码的实体列表。但是没有提到空间特征。您可能想尝试调用Uri.UnescapeDataString()。
:)
https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx