2015-11-07 149 views
0

這是我的客戶端代碼(WinForm的C#桌面):System.Net.WebUtility.HtmlDecode似乎沒有正常工作

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

這在我的服務器應用程序的代碼,這在UWP應用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())); 

,這是結果:

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 

,你可以看到 '<' 和 '>' AR e轉換成OK,但空格仍然被讀爲%20?

回答