#region 获取网页源码
public string getWebSouce(string url)
{
WebClient client = new WebClient();
try
{
_websource = client.DownloadString(new Uri(url));
}
catch
{
_websource = "";
}
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = System.Text.RegularExpressions.Regex.Match(_websource, pattern).Groups["charset"].Value;
try
{
string tmp = _websource;
Encoding encode = Encoding.GetEncoding(charset);
_websource = encode.GetString(client.Encoding.GetBytes(tmp.ToCharArray()));
//转为utf-8编码
// _websource = Encoding.UTF8.GetString(encode.GetBytes(tmp.ToCharArray())); }
catch
{ }
//client.DownloadStringAsync(new Uri(url));
// client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
return _websource;
}大师们,能不能帮解释一下这段代码呢
尤其是正则部分
public string getWebSouce(string url)
{
WebClient client = new WebClient();
try
{
_websource = client.DownloadString(new Uri(url));
}
catch
{
_websource = "";
}
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = System.Text.RegularExpressions.Regex.Match(_websource, pattern).Groups["charset"].Value;
try
{
string tmp = _websource;
Encoding encode = Encoding.GetEncoding(charset);
_websource = encode.GetString(client.Encoding.GetBytes(tmp.ToCharArray()));
//转为utf-8编码
// _websource = Encoding.UTF8.GetString(encode.GetBytes(tmp.ToCharArray())); }
catch
{ }
//client.DownloadStringAsync(new Uri(url));
// client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
return _websource;
}大师们,能不能帮解释一下这段代码呢
尤其是正则部分
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货