Repository navigation
Expand file tree
/
Copy pathHttp.cs
More file actions
240 lines (216 loc) · 7.1 KB
/
Copy pathHttp.cs
File metadata and controls
240 lines (216 loc) · 7.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Globalization;
namespace RevStack.Net
{
public static class Http
{
#region "public"
public static string Get(string address)
{
try
{
var result = "";
using (var client = new WebClient())
{
using (var reader = new StreamReader(client.OpenRead(address)))
{
var s = reader.ReadToEnd();
result = s;
}
}
return result;
}
catch (Exception)
{
return "";
}
}
public static HttpResult Get(string address, string baseUrl)
{
var httpResult = new HttpResult();
try
{
var result = "";
using (var client = new WebClient())
{
using (var reader = new StreamReader(client.OpenRead(address)))
{
var s = reader.ReadToEnd();
result = s;
}
}
httpResult.Html = result;
httpResult.StatusCode = HttpStatusCode.OK;
httpResult.Error = "";
return httpResult;
}
catch (Exception e)
{
httpResult.Html = "";
if (e.GetType().Name == "WebException")
{
var we = (WebException)e;
var response = (HttpWebResponse)we.Response;
httpResult.StatusCode = response.StatusCode;
httpResult.Error = we.Message;
}
else
{
httpResult.StatusCode = HttpStatusCode.InternalServerError;
httpResult.Error = e.Message;
}
return httpResult;
}
}
public static HttpResult Get(string address, string username, string password)
{
var httpResult = new HttpResult();
try
{
var result = "";
using (var client = new WebClient())
{
client.Headers.Add("Authorization",
"Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)));
using (var reader = new StreamReader(client.OpenRead(address)))
{
var s = reader.ReadToEnd();
result = s;
}
}
httpResult.Html = result;
httpResult.StatusCode = HttpStatusCode.OK;
httpResult.Error = "";
return httpResult;
}
catch (Exception e)
{
httpResult.Html = "";
if (e.GetType().Name == "WebException")
{
var we = (WebException)e;
var response = (HttpWebResponse)we.Response;
httpResult.StatusCode = response.StatusCode;
httpResult.Error = we.Message;
}
else
{
httpResult.StatusCode = HttpStatusCode.InternalServerError;
httpResult.Error = e.Message;
}
return httpResult;
}
}
public static bool IsValidEmail(string strIn)
{
if (String.IsNullOrEmpty(strIn))
return false;
// Use IdnMapping class to convert Unicode domain names.
try
{
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException)
{
return false;
}
if (!isValidDomain)
return false;
// Return true if strIn is in valid e-mail format.
try
{
return Regex.IsMatch(strIn,
@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException)
{
return false;
}
}
public static string ConvertPathToUrl(string path, string domain)
{
var url = "";
if (path.IndexOf("http://") == -1)
{
url = "http://" + domain + path;
}
else
{
url = path;
}
return path;
}
public static string ConvertPathToUrl(string path, string domain, string port)
{
var url = "";
if (path.IndexOf("http://") == -1)
{
url = "http://" + domain + ":" + port + path;
}
else
{
url = path;
}
return path;
}
public static string ConvertPathToUrl(string path, string domain, string host, string port)
{
var url = "";
if (path.IndexOf("http://") == -1)
{
url = "http://" + host + "." + domain + ":" + port + path;
}
else
{
url = path;
}
return path;
}
public static BasicCredentials DecodeBasicAuthorization(string auth)
{
var basicCredentials = new BasicCredentials();
try
{
var decoded = Encoding.ASCII.GetString(Convert.FromBase64String(auth));
var authArray = decoded.Split(':');
var user = authArray[0];
var pass = authArray[1];
basicCredentials.Username = user;
basicCredentials.Password = pass;
basicCredentials.Error = false;
return basicCredentials;
}
catch (Exception)
{
basicCredentials.Error = true;
return basicCredentials;
}
}
#endregion
#region "private"
private static bool isValidDomain = true;
private static string DomainMapper(Match match)
{
// IdnMapping class with default property values.
var idn = new IdnMapping();
var domainName = match.Groups[2].Value;
try
{
domainName = idn.GetAscii(domainName);
}
catch (ArgumentException)
{
isValidDomain = false;
}
return match.Groups[1].Value + domainName;
}
#endregion
}
}