zoukankan      html  css  js  c++  java
  • C# 解决httplistener querystring 中文乱码、返回json中文格式乱码

    解决httplistener querystring 中文乱码方案:

    在请求到达时候,获取Request.Url,返回get请求参数 键值对

        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中文格式乱码:

    对中午json字符串进行编码 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);
                //关闭输出流,释放相应资源
                output.Close();
                response.Close();
            }
        }
    

    转载于:链接

  • 相关阅读:
    jQuery 在 IE 上 clone checkbox 的問題。
    C/C++ typedef用法
    C++继承
    map常用操作
    C++ JsonCpp 使用(含源码下载)
    string常用操作
    C++虚函数
    STL容器迭代过程中删除元素技巧(转)
    关于IE下用HTTPS无法下载/打开文件(转)
    C++STL概览
  • 原文地址:https://www.cnblogs.com/cxfs/p/14763496.html
Copyright © 2011-2022 走看看