Skip to content

Commit af311aa

Browse files
committed
Refactor HttpClient usage for consistency and efficiency
1 parent 05dbb61 commit af311aa

3 files changed

Lines changed: 15 additions & 23 deletions

File tree

ExtLibs/Utilities/Download.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ public class Download
303303
private static readonly ILog log =
304304
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
305305

306+
static readonly HttpClient client = new HttpClient();
307+
306308
public static async Task<string> PostAsync(string uri, string data)
307309
{
308-
var httpClient = new HttpClient();
309-
var response = await httpClient.PostAsync(uri, new StringContent(data));
310+
var request = new HttpRequestMessage(HttpMethod.Post, uri) { Content = new StringContent(data) };
311+
var response = await client.SendAsync(request);
310312

311313
response.EnsureSuccessStatusCode();
312314

@@ -316,8 +318,9 @@ public static async Task<string> PostAsync(string uri, string data)
316318

317319
public static async Task<string> GetAsync(string uri)
318320
{
319-
var httpClient = new HttpClient();
320-
var content = await httpClient.GetStringAsync(uri);
321+
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
322+
response.EnsureSuccessStatusCode();
323+
var content = await response.Content.ReadAsStringAsync();
321324
return await Task.Run(() => (content));
322325
}
323326

@@ -332,8 +335,7 @@ public struct HTTPResult
332335
/// </summary>
333336
public static async Task<HTTPResult> GetAsyncWithStatus(string uri)
334337
{
335-
var httpClient = new HttpClient();
336-
var response = await httpClient.GetAsync(uri);
338+
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
337339
var content = await response.Content.ReadAsStringAsync();
338340
return await Task.Run(() => (new HTTPResult() { content = content, status = response.StatusCode }));
339341
}
@@ -347,7 +349,6 @@ public static async Task<bool> getFilefromNetAsync(string url, string saveto, Ac
347349
log.Info("Get " + url);
348350

349351
var request = new HttpRequestMessage(HttpMethod.Get, url);
350-
351352
RequestModification?.Invoke(url, request);
352353

353354
using (var response = await client.SendAsync(request, completionOption: HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
@@ -444,18 +445,15 @@ static Download()
444445
{
445446
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
446447
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
448+
client.Timeout = TimeSpan.FromSeconds(30);
447449
}
448450

449-
static HttpClient client = new HttpClient();
450451
public static bool getFilefromNet(string url, string saveto, Action<int, string> status = null)
451452
{
452453
try
453454
{
454455
lock (log)
455456
log.Info(url);
456-
var client = new HttpClient();
457-
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
458-
client.Timeout = TimeSpan.FromSeconds(30);
459457

460458
// Get the response.
461459
var response = client.GetAsync(url, completionOption: HttpCompletionOption.ResponseHeadersRead).Result;
@@ -569,9 +567,6 @@ public static bool CheckHTTPFileExists(string url)
569567
if (url == null || url == "" || uri == null)
570568
return false;
571569

572-
var client = new HttpClient();
573-
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
574-
client.Timeout = TimeSpan.FromSeconds(30);
575570
var resp = client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)).Result;
576571
return resp.IsSuccessStatusCode;
577572

ExtLibs/Utilities/Tracking.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static Guid cid
6060

6161
static Tracking()
6262
{
63+
client.Timeout = TimeSpan.FromSeconds(30);
6364
}
6465

6566
public static void AddEvent(string cat, string action, string label, string value)
@@ -102,6 +103,8 @@ public static string productVersion
102103
get; set;
103104
}
104105

106+
static readonly HttpClient client = new HttpClient();
107+
105108
public static string productName
106109
{
107110
get; set;
@@ -297,10 +300,6 @@ static void track(object temp)
297300

298301
try
299302
{
300-
var client = new HttpClient();
301-
client.DefaultRequestHeaders.Add("User-Agent", productName + " " + productVersion + " (" + Environment.OSVersion.VersionString + ")");
302-
client.Timeout = TimeSpan.FromSeconds(30);
303-
304303
string data = "";
305304

306305
List<KeyValuePair<string, string>> data1 = (List<KeyValuePair<string, string>>)temp;
@@ -321,7 +320,9 @@ static void track(object temp)
321320

322321
log.Debug(data);
323322

324-
client.PostAsync(secureTrackingEndpoint, new StringContent(data));
323+
var request = new HttpRequestMessage(HttpMethod.Post, secureTrackingEndpoint) { Content = new StringContent(data) };
324+
request.Headers.TryAddWithoutValidation("User-Agent", productName + " " + productVersion + " (" + Environment.OSVersion.VersionString + ")");
325+
client.SendAsync(request);
325326
}
326327
catch { }
327328
}

ExtLibs/Utilities/adsb.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ void TryConnect()
142142
{
143143
// ADSB Exchange API format - see https://api.adsb.lol/docs
144144
string url = "{0}/v2/point/{1}/{2}/{3}";
145-
Download.RequestModification += (u, request) => {
146-
// for future use if necessary: request.Headers.Add("X-API-Auth", "example");
147-
request.SetHeader("User-Agent", "Mission-Planner/" + ApplicationVersion);
148-
};
149145
var delay = API_LOOP_DELAY_MILLISECONDS;
150146

151147
while (true)

0 commit comments

Comments
 (0)