日期:2014-05-18  浏览次数:20773 次

读取远程文件,返回乱码,请大家帮忙解决
返回后,string   yuancheng接收到的字符,很多是乱码,大家帮忙解决一下,谢谢


using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;
using   System.Net;

using   System.Text.RegularExpressions;
using   System.IO;

namespace   WindowsApplication2
{
        public   partial   class   Form1   :   Form
        {
                public   Form1()
                {
                        InitializeComponent();
                }

                private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        string   url   =   "http://d.baidu.com/rs.php?q=山东 ";
                       
                        //创建请求对象
                        HttpWebRequest   request   =   (HttpWebRequest)HttpWebRequest.Create(url);
                        //接收(回应)请求对象返回的信息,也就是源代码
                        HttpWebResponse   response   =   (HttpWebResponse)request.GetResponse();
                        //最后,将获取的信息读出来
                        StreamReader   sr   =   new   StreamReader(response.GetResponseStream(),Encoding.GetEncoding   ( "gb2312 "));
                        //读取内容到字符串
                        string   yuancheng   =   sr.ReadToEnd();     //**取到的值中间部分乱码**
                        //关闭读取流,并释放资源
                        response.Close();
                        sr.Close();
                }
        }
}

------解决方案--------------------
Encoding.GetEncoding ( "gb2312 "))

你读去的辑码要和文档的编码一致,你可以试试UTF-8