TextBox2.Text = getHtmlfromURL("http://www.baidu.com/s?wd=" +Server.UrlEncode("测试"));
     string getHtmlfromURL(string urlstr)
        {
            //WebRequest wrt = WebRequest.Create(urlstr);
            //WebResponse wrse = wrt.GetResponse();
            //Stream strM = wrse.GetResponseStream();
            //StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("gb2312"));
            //string strallstrm = SR.ReadToEnd();
            //return strallstrm;             System.Net.WebClient web = new System.Net.WebClient();
            byte[] b = web.DownloadData(urlstr);
            string str = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
            return str;        }
我的代码如下,可是返回的搜索关键字总是乱码,请各位指点,谢谢!!!

解决方案 »

  1.   


    string str = System.Text.Encoding.GetEncoding("gb2312").GetString(b);看看是不是utf-8编码
      

  2.   

    你对那个字符串进行了编码。引用得时候需要解码Server.UrlDecode("字符串")
      

  3.   

    TextBox2.Text = getHtmlfromURL("http://www.baidu.com/s?wd=测试";
      string getHtmlfromURL(string urlstr)
      {
      System.Net.WebClient web = new System.Net.WebClient();
      byte[] b = web.DownloadData(urlstr);
      string str = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
      return str;
      }
    这样还是不行。
      

  4.   

    GetEncoding("gb2312")改成GetEncoding("utf-8")试试
      

  5.   

    "http://www.baidu.com/s?wd=测试"+
    HttpUtility.UrlEncode("测试",System.Text.Encoding.GetEncoding("Gb2312"))
    ---------
    指定以GB2312进行URLEncode