首页 新闻 会员 周边

POST请求结果是乱码是怎么回事?

0
[已解决问题] 解决于 2023-04-06 13:51

 

 

public static string GetBaiDuExtKey_post(string key)
        {
            string strURL = String.Format("http://suggestion.baidu.com/su?wd={0}&sugmode=3&json=1", key);
            //json参数
            string jsonParam = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {

            });
            var request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
            int length = byteData.Length;
            request.ContentLength = length;
            Stream writer = request.GetRequestStream();
            writer.Write(byteData, 0, length);
            writer.Close();
            var response = (HttpWebResponse)request.GetResponse();
            //var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();

            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = reader.ReadToEnd();

            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            MessageBox.Show(result);
            return result.ToString();
        }

 

复制代码
这段代码运行结果如上图,是乱码是怎么回事?post请求也试了也不行
public
KeyWordModel GetBaiDuExtKey(string key) { string strURL = String.Format("http://suggestion.baidu.com/su?wd={0}&sugmode=3&json=1", key); System.Net.HttpWebRequest request; // 创建一个HTTP请求 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //request.Method="get"; System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseText = myreader.ReadToEnd(); myreader.Close(); MessageBox.Show(responseText); return null; }
复制代码
大da脸的主页 大da脸 | 初学一级 | 园豆:73
提问于:2023-04-06 11:32
< >
分享
最佳答案
0

Content-Type: text/javascript; charset=gbk

人家返回的gkb, 你用gbk解码.

奖励园豆:5
czd890 | 专家六级 |园豆:14414 | 2023-04-06 13:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册