forked from XIU2/TileTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebClient.cs
63 lines (58 loc) · 1.66 KB
/
WebClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.IO;
using System.Net;
namespace WebClient_cs
{
public class GetHTTP
{
/// <summary>
/// 获取HTTP
/// </summary>
/// <param name="URL">URL</param>
/// <param name="TimeOut">超时时间,默认:60000 毫秒,单位:毫秒</param>
/// <param name="Encoding">编码,默认:utf-8</param>
/// <returns>成功返回网页内容,失败返回 null</returns>
public static string Get_HTTP(string Url, int TimeOut, string Encoding = "utf-8")
{
NewWebClient myWebClient = new NewWebClient(TimeOut);
Stream myStream = myWebClient.OpenRead(Url);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(Encoding));
string strHTML = sr.ReadToEnd();
myStream.Close();
return strHTML;
}
}
// 带超时时间的 WebClient
public class NewWebClient : WebClient
{
private int _timeout;
/// <summary>
/// 超时时间(毫秒)
/// </summary>
public int Timeout
{
get
{
return _timeout;
}
set
{
_timeout = value;
}
}
public NewWebClient()
{
this._timeout = 60000;
}
public NewWebClient(int timeout)
{
this._timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
}
}
}