关于Http请求GBK乱码转化的问题

    xiaoxiao2022-07-06  211

    这种情况的站点不常见,但是也会偶尔有出现的,目前遇到站点的有北京市国家税务局、深圳信用网租赁信息 特此做个记录,下面直接给大家上下代码,以C#为例:

    class POST请求GBK乱码转化 { public void Start() { //将需要传入的中文参数转化为GBK格式 string INFO = System.Web.HttpUtility.UrlEncode("北京市国家税务局", Encoding.GetEncoding("GBK")); string info = System.Web.HttpUtility.UrlDecode(INFO, Encoding.GetEncoding("GBK")); Console.WriteLine(INFO + "\n" + info); try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create("请求地址"); //增加编码格式为GBK req.ContentType = "application/x-www-form-urlencoded;charset=GBK"; req.Method = "POST"; req.ServicePoint.Expect100Continue = false; string body = INFO; byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body); req.ContentLength = postBytes.Length; Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); var response = (HttpWebResponse)req.GetResponse(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
    最新回复(0)