我在尝试从 google.com 获取 html 时遇到编码问题。请给我一个建议如何解决这个问题。非常感谢。
public string Html
{
get
{
try
{
var request = WebRequest.Create(Url) as HttpWebRequest;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1,gzip(gfe)";
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
string Charset = response.CharacterSet;
Encoding encoding = Encoding.GetEncoding(Charset);
var sr = new StreamReader(response.GetResponseStream(), encoding);
return sr.ReadToEnd();
}
}
return string.Format("Could not create object HttpWebRequest for '{0}'", Url);
}
catch (Exception e)
{
return e.Message;
}
}
}
这里还有一张图片: