C # solve the Chinese garbled code of httplistener querystring and returned JSON Chinese format

Time:2021-9-5

Solution to httplistener querystring Chinese garbled Code:

When the request arrives, get the request. URL and return the get request parameter key value pair

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

Solve the problem of returned JSON Chinese format garbled Code:

Encode the noon JSON string httputility.urldecode (“Chinese”);

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);
            //Close the output stream and release the corresponding resources
            output.Close();
            response.Close();
        }
    }

Reproduced at:link

Recommended Today

Git related configuration

Configure user name and mailbox //Configure user name git config –global user.name “username” //Configure mailbox git config –global user.email “[email protected]” Generate SSH key ssh-keygen -t rsa -C “[email protected]” Ignore changes in file permissions or file owners #Current version Library git config core.filemode false #Global configuration git config –global core.fileMode false ssh Git public key This […]