黄聡:C#のWebClientは自動的にコードがUTF-8かGBKかを判断して、しかもタイムアウトの判断機能があります
6195 ワード
public class WebDownload : WebClient
{
private int _timeout;
/// <summary>
/// ( )
/// </summary>
public int Timeout
{
get
{
return _timeout;
}
set
{
_timeout = value;
}
}
public WebDownload()
{
this._timeout = 60000;
}
public WebDownload(int timeout)
{
this._timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
}
public string Get(string url)
{
string html = "";
var data = this.DownloadData(url);
var r_utf8 = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.UTF8); // html utf8 StreamReader
var r_gbk = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.Default); // html gbk StreamReader
var t_utf8 = r_utf8.ReadToEnd(); // html
var t_gbk = r_gbk.ReadToEnd(); // html
if (!isLuan(t_utf8)) // utf8
{
html = t_utf8;
}
else
{
html = t_gbk;
}
return html;
}
bool isLuan(string txt)
{
var bytes = Encoding.UTF8.GetBytes(txt);
//239 191 189
for (var i = 0; i < bytes.Length; i++)
{
if (i < bytes.Length - 3)
if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189)
{
return true;
}
}
return false;
}
}