Skip to content

Commit

Permalink
[Core] Replace WebRequest with HttpClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
claunia committed May 2, 2024
1 parent 6a3ef12 commit 70001d8
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions Aaru.Core/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -360,31 +360,20 @@ static async Task SubmitStats()
#else
Aaru.Console.AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Core.Uploading_statistics);
#endif
string json = JsonSerializer.Serialize(dto, typeof(StatsDto), StatsDtoContext.Default);
using StringContent jsonContent =
new(JsonSerializer.Serialize(dto, typeof(StatsDto), StatsDtoContext.Default),
Encoding.UTF8,
"application/json");

byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
var request = WebRequest.Create("https://www.aaru.app/api/uploadstatsv2");
var client = new HttpClient();
client.BaseAddress = new Uri("https://www.aaru.app");
client.DefaultRequestHeaders.Add("User-Agent", $"Aaru {typeof(Version).Assembly.GetName().Version}");

((HttpWebRequest)request).UserAgent = $"Aaru {typeof(Version).Assembly.GetName().Version}";
using HttpResponseMessage response = await client.PostAsync("/api/uploadstatsv2", jsonContent);

request.Method = "POST";
request.ContentLength = jsonBytes.Length;
request.ContentType = "application/json";
Stream reqStream = await request.GetRequestStreamAsync();
await reqStream.WriteAsync(jsonBytes, 0, jsonBytes.Length);
if(response.StatusCode != HttpStatusCode.OK) return;

//jsonStream.CopyTo(reqStream);
reqStream.Close();
WebResponse response = await request.GetResponseAsync();

if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) return;

Stream data = response.GetResponseStream();
var reader = new StreamReader(data ?? throw new InvalidOperationException());

string result = await reader.ReadToEndAsync();
data.Close();
response.Close();
string result = await response.Content.ReadAsStringAsync();

if(result != "ok") return;

Expand Down

0 comments on commit 70001d8

Please sign in to comment.