110,534
社区成员
发帖
与我相关
我的任务
分享
public static string GetHtmlAutoEncoding()
{
HttpWebRequest req = null;
HttpWebResponse resp = null;
Stream stream = null;
StreamReader read = null;
try
{
string url = "http://roll.sohu.com/20110827/n317536952.shtml";
req = (HttpWebRequest)HttpWebRequest.Create(sUrl);
req.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:6.0) Gecko/20100101 Firefox/6.0";
req.Accept = "*/*";
req.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
req.ContentType = "text/xml";
req.Referer = url;
resp = (HttpWebResponse)req.GetResponse();
Encoding enc = Encoding.GetEncoding(resp.CharacterSet);
string sHTML = string.Empty;
stream = resp.GetResponseStream();
string sChartSet = "";
read = new StreamReader(stream, enc);
sHTML = read.ReadToEnd();
Match charSetMatch = Regex.Match(sHTML, @"charset=(?<code>[\s\S]+?)", RegexOptions.IgnoreCase);
sChartSet = charSetMatch.Groups["code"].Value;
//if it's not utf-8,we should redecode the html.
if (!string.IsNullOrEmpty(sChartSet.Trim()))
sHTML = Encoding.GetEncoding(sChartSet).GetString(enc.GetBytes(sHTML));
if (resp.CharacterSet != "iso-8859-1")
{
Encoding encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
byte[] byteArray = encoding.GetBytes(sHTML);
string aaXX = System.Text.Encoding.GetEncoding("GBK").GetString(byteArray);
}
return sHTML;
}
catch
{
return "";
}
finally
{
if (resp != null)
{
resp.Close();
}
if (stream != null)
{
stream.Close();
}
if (read != null)
{
read.Close();
}
}
}