解決httplistener querystring 中文亂碼方案:
在請(qǐng)求到達(dá)時(shí)候蝴韭,獲取Request.Url,返回get請(qǐng)求參數(shù) 鍵值對(duì)
public class RequestHelper
{
public static Dictionary<string, string> EncodeQueryString(Uri uri)
{
var ret = new Dictionary<string, string>();
var q = uri.Query;
if (q.Length > 0)
{
foreach (var p in q.Substring(1).Split('&'))
{
var s = p.Split(new char[] { '=' }, 2);
ret.Add(HttpUtility.UrlDecode(s[0]), HttpUtility.UrlDecode(s[1]));
}
}
return ret;
}
}
解決返回json中文格式亂碼:
對(duì)中午json字符串進(jìn)行編碼 HttpUtility.UrlDecode(“中文”);
public class ResponseHelper
{
public static void Respose(HttpListenerResponse response, string jsonStr = "")
{
byte[] buffer = Encoding.UTF8.GetBytes(jsonStr);
response.ContentLength64 = buffer.Length;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
response.StatusCode = 200;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
//關(guān)閉輸出流,釋放相應(yīng)資源
output.Close();
response.Close();
}
}
轉(zhuǎn)載于:鏈接