一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁編碼

復(fù)制代碼 代碼如下:
static string GetEncoding(string url)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;

response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
else
reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);

string html = reader.ReadToEnd();

Regex reg_charset = new Regex(@"charset/b/s*=/s*(?<charset>[^""]*)");
if (reg_charset.IsMatch(html))
{
return reg_charset.Match(html).Groups["charset"].Value;
}
else if (response.CharacterSet != string.Empty)
{
return response.CharacterSet;
}
else
return Encoding.Default.BodyName;
}
}
catch
{
}
finally
{

if (response != null)
{
response.Close();
response = null;
}
if (reader != null)
reader.Close();

if (request != null)
request = null;

}

return Encoding.Default.BodyName;
}

/// <summary>
/// 獲取源代碼
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
static string GetHtml(string url, Encoding encoding)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;

response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding);
else
reader = new StreamReader(response.GetResponseStream(), encoding);
string html = reader.ReadToEnd();

return html;
}
}
catch
{
}
finally
{

if (response != null)
{
response.Close();
response = null;
}
if (reader != null)
reader.Close();

if (request != null)
request = null;

}

return string.Empty;
}

AspNet技術(shù)asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁編碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产美女精品一区二区三区 | 久久青青草原热精品 | 夜夜未满十八勿进的爽爽影院 | 91久久国产综合精品女同国语 | 美女视频永久黄网站免费观看韩国 | 欧美一级免费 | 久久综久久美利坚合众国 | 999影院成 人在线影院 | 国产精品第7页 | 加勒比精品 | 国产亚洲福利精品一区二区 | 国产精品麻豆一区二区三区v视界 | 热99re久久精品香蕉 | 99在线观看精品视频 | 91久久精品国产免费一区 | 久久中文字幕视频 | 国产综合久久久久久 | 国产在线永久视频 | 亚洲国产成人在线视频 | 精品玖玖玖视频在线观看 | 看全色黄大色黄大片视频 | 91精品国产免费久久久久久 | 91福利视频免费 | 成人精品在线观看 | 国产在线拍| 在线精品国产第一页 | 亚洲图片欧美在线 | 福利一区二区在线观看 | 成人怡红院视频在线观看 | 久久国产高清视频 | 久久免费视频6 | 婷婷六月丁香午夜爱爱 | 91精品国产91久久久久福利 | 露脸国产自产拍在线观看 | 精品视频一区二区三区 | 亚洲综合激情另类专区 | 51短视频版在线观看www免费 | 国产成人亚洲精品91专区手机 | 亚洲图片第一页 | 欧美午夜色大片在线观看免费 | 亚洲五月婷婷 |