-
Notifications
You must be signed in to change notification settings - Fork 7
/
HttpHeader.cs
55 lines (47 loc) · 1.73 KB
/
HttpHeader.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
using System;
using System.Net;
using System.Diagnostics;
namespace SteamHosts
{
class HttpHeader
{
private static string ERROR_TIMEOUT = "连接超时";
private static string ERROR_RESET = "连接被重置";
private static string ERROR_OTHER = "连接失败";
public static HttpResult GetHttpConnectionStatus(
string hostname, string ip, int timeout)
{
HttpResult result = new HttpResult();
result.setFlag(false);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + hostname + "/");
request.Timeout = 1000 * timeout;
if (! string.IsNullOrEmpty(ip))
{
WebProxy proxy = new WebProxy(ip);
request.Proxy = proxy;
}
Stopwatch timer = new Stopwatch();
timer.Start();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
timer.Stop();
TimeSpan timeSpan = timer.Elapsed;
result.setTime((int) timeSpan.TotalMilliseconds);
result.setFlag(true);
} catch (WebException e) when (e.Status == WebExceptionStatus.Timeout)
{
result.setResult(ERROR_TIMEOUT);
} catch (WebException e) when (e.Status == WebExceptionStatus.ReceiveFailure)
{
result.setResult(ERROR_RESET);
} catch (WebException e)
{
Console.WriteLine(e);
result.setResult(ERROR_OTHER);
}
return result;
}
}
}