Repository navigation
Expand file tree
/
Copy pathWebClient.cs
More file actions
271 lines (245 loc) · 10.6 KB
/
Copy pathWebClient.cs
File metadata and controls
271 lines (245 loc) · 10.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
using CDBurnerXP;
using System.Net.Http;
using System.Threading.Tasks;
namespace Ketarin
{
/// <summary>
/// The modified WebClient used by Ketarin for
/// downloads. Submits a valid user agent by default.
/// </summary>
internal class WebClient : System.Net.WebClient
{
private static string? defaultUserAgent;
private string replacementString = string.Empty;
private HttpClient httpClient = new HttpClient();
#region Properties
/// <summary>
/// If the WebClient has been redirected after a request,
/// this specifies the new URL.
/// </summary>
public Uri? ResponseUri { get; private set; }
/// <summary>
/// Gets the plain POST data which is being ursed for a request.
/// </summary>
public string PostData { get; private set; } = string.Empty;
/// <summary>
/// Default user agent for all requests.
/// </summary>
public static string DefaultUserAgent
{
get { return defaultUserAgent ?? (defaultUserAgent = Settings.GetValue("DefaultUserAgent", "Mozilla/4.0 (compatible; Ketarin; +https://ketarin.org/)") as string ?? string.Empty); }
set { defaultUserAgent = value; }
}
#endregion
public WebClient()
: this(null)
{
// Configure HttpClient with default settings
httpClient.Timeout = TimeSpan.FromSeconds(Convert.ToInt32(Settings.GetValue("ConnectionTimeout", 10)));
}
public WebClient(string? userAgent)
{
// Configure HttpClient with user agent
string userAgentValue = string.IsNullOrEmpty(userAgent) ? DefaultUserAgent : userAgent ?? string.Empty;
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgentValue);
// Configure other default headers
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
// MS Bugfix - https://connect.microsoft.com/VisualStudio/feedback/details/386695/system-uri-incorrectly-strips-trailing-dots?wa=wsignin1.0#
MethodInfo? getSyntax = typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo? flagsField = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
object? parserObj = getSyntax.Invoke(null, new object[] { scheme });
UriParser? parser = parserObj as UriParser;
if (parser != null)
{
object? flagsValueObj = flagsField.GetValue(parser);
int flagsValue = flagsValueObj != null ? (int)flagsValueObj : 0;
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
}
/// <summary>
/// Downloads a string from the specified URI using HttpClient.
/// </summary>
/// <param name="address">The URI to download from.</param>
/// <returns>The downloaded string.</returns>
public new async Task<string> DownloadStringTaskAsync(string address)
{
try
{
return await DownloadStringTaskAsync(new Uri(address));
}
catch (UriFormatException)
{
throw new UriFormatException("The format of the URI \"" + address + "\" cannot be determined.");
}
}
/// <summary>
/// Downloads a string from the specified URI using HttpClient.
/// </summary>
/// <param name="address">The URI to download from.</param>
/// <returns>The downloaded string.</returns>
public new async Task<string> DownloadStringTaskAsync(Uri address)
{
this.replacementString = string.Empty;
try
{
// Handle POST data if present
if (!string.IsNullOrEmpty(this.PostData))
{
var content = new StringContent(this.PostData, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(address, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
else
{
// Simple GET request
return await httpClient.GetStringAsync(address);
}
}
catch (HttpRequestException ex)
{
// Handle SSL/TLS issues
if (ex.InnerException is System.Security.Authentication.AuthenticationException)
{
// Try with different security protocols
var oldHandler = httpClient;
httpClient = new HttpClient(new HttpClientHandler()
{
SslProtocols = System.Security.Authentication.SslProtocols.Tls12
});
httpClient.Timeout = oldHandler.Timeout;
try
{
if (!string.IsNullOrEmpty(this.PostData))
{
var content = new StringContent(this.PostData, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(address, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
else
{
return await httpClient.GetStringAsync(address);
}
}
finally
{
// Restore default protocols
httpClient = oldHandler;
}
}
if (!string.IsNullOrEmpty(this.replacementString))
{
return this.replacementString;
}
throw;
}
}
/// <summary>
/// Downloads a string from the specified URI using HttpClient (synchronous wrapper).
/// </summary>
/// <param name="address">The URI to download from.</param>
/// <returns>The downloaded string.</returns>
public new string DownloadString(string address)
{
return DownloadStringTaskAsync(address).GetAwaiter().GetResult();
}
/// <summary>
/// Downloads a string from the specified URI using HttpClient (synchronous wrapper).
/// </summary>
/// <param name="address">The URI to download from.</param>
/// <returns>The downloaded string.</returns>
public new string DownloadString(Uri address)
{
return DownloadStringTaskAsync(address).GetAwaiter().GetResult();
}
/// <summary>
/// Gets the response for the specified request using HttpClient.
/// </summary>
/// <param name="request">The request to get response for.</param>
/// <returns>The response for the request.</returns>
public static async Task<HttpResponseMessage> GetResponseAsync(HttpRequestMessage request)
{
using (var client = new HttpClient())
{
return await client.SendAsync(request);
}
}
/// <summary>
/// Sets the POST data for this WebClient based on a UrlVariable.
/// </summary>
/// <param name="variable">The UrlVariable containing POST data.</param>
public void SetPostData(UrlVariable variable)
{
this.PostData = variable?.PostData ?? string.Empty;
}
/// <summary>
/// Parses POST data string into key-value pairs.
/// </summary>
/// <param name="postData">The POST data string to parse.</param>
/// <returns>An array of string arrays, each containing a key-value pair.</returns>
public static string[][] GetKeyValuePairs(string postData)
{
if (string.IsNullOrEmpty(postData))
return new string[0][];
var pairs = new List<string[]>();
string[] keyValuePairs = postData.Split('&');
foreach (string pair in keyValuePairs)
{
string[] keyValue = pair.Split('=');
if (keyValue.Length >= 2)
{
pairs.Add(new[] {
System.Web.HttpUtility.UrlDecode(keyValue[0]),
System.Web.HttpUtility.UrlDecode(keyValue[1])
});
}
else if (keyValue.Length == 1)
{
pairs.Add(new[] {
System.Web.HttpUtility.UrlDecode(keyValue[0]),
string.Empty
});
}
}
return pairs.ToArray();
}
/// <summary>
/// Gets the response for the specified WebRequest.
/// </summary>
/// <param name="request">The WebRequest to get response for.</param>
/// <returns>The WebResponse for the request.</returns>
public static WebResponse GetResponse(WebRequest request)
{
// This is a synchronous wrapper around the async method
return request.GetResponse();
}
/// <summary>
/// Disposes of the HttpClient resources.
/// </summary>
/// <param name="disposing">True if called from Dispose(), false if called from finalizer.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
httpClient?.Dispose();
}
base.Dispose(disposing);
}
}
}