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; }
Content-Type: text/javascript; charset=gbk
人家返回的gkb, 你用gbk解码.