Improve HttpHelper body handling and error reporting
HttpHelper.FetchAsync now supports HttpContent as a body parameter, allowing for more flexible request content. In MojangAPI.UploadSkinAsync, error messages now include the response body for better debugging.
This commit is contained in:
parent
15adb39115
commit
ee1e241e8e
@ -10,8 +10,6 @@ public static class HttpHelper {
|
|||||||
public static async Task<HttpResponseMessage> FetchAsync(string url, HttpMethod? method = null, object? body = null, Dictionary<string, string>? headers = null) {
|
public static async Task<HttpResponseMessage> FetchAsync(string url, HttpMethod? method = null, object? body = null, Dictionary<string, string>? headers = null) {
|
||||||
var request = new HttpRequestMessage(method ?? HttpMethod.Get, url);
|
var request = new HttpRequestMessage(method ?? HttpMethod.Get, url);
|
||||||
|
|
||||||
request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
|
||||||
|
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
foreach (var header in headers) {
|
foreach (var header in headers) {
|
||||||
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
|
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
|
||||||
@ -19,13 +17,17 @@ public static class HttpHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
if (body is string s) {
|
if (body is HttpContent httpContent) {
|
||||||
|
request.Content = httpContent;
|
||||||
|
} else if (body is string s) {
|
||||||
request.Content = new StringContent(s, System.Text.Encoding.UTF8, "application/json");
|
request.Content = new StringContent(s, System.Text.Encoding.UTF8, "application/json");
|
||||||
} else {
|
} else {
|
||||||
request.Content = JsonContent.Create(body, options: LauncherConstants._jsonOptions);
|
request.Content = JsonContent.Create(body, options: LauncherConstants._jsonOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||||
|
|
||||||
return await Http.SendAsync(request);
|
return await Http.SendAsync(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,19 +9,27 @@ public static class MojangAPI {
|
|||||||
|
|
||||||
public static async Task<string> UploadSkinAsync(string filePath, string variant, string token) {
|
public static async Task<string> UploadSkinAsync(string filePath, string variant, string token) {
|
||||||
using var content = new MultipartFormDataContent();
|
using var content = new MultipartFormDataContent();
|
||||||
|
|
||||||
content.Add(new StringContent(variant), "variant");
|
content.Add(new StringContent(variant), "variant");
|
||||||
|
|
||||||
byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
|
byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
|
||||||
var fileContent = new ByteArrayContent(fileBytes);
|
var fileContent = new ByteArrayContent(fileBytes);
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
|
||||||
|
|
||||||
content.Add(fileContent, "file", Path.GetFileName(filePath));
|
content.Add(fileContent, "file", Path.GetFileName(filePath));
|
||||||
|
|
||||||
var response = await HttpHelper.FetchAsync("https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/skins", method: HttpMethod.Post, body: content, headers: new() { { "Authorization", $"Bearer {token}" } } );
|
var response = await HttpHelper.FetchAsync(
|
||||||
|
"https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/skins",
|
||||||
|
method: HttpMethod.Post,
|
||||||
|
body: content,
|
||||||
|
headers: new() { { "Authorization", $"Bearer {token}" } }
|
||||||
|
);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode) {
|
if (response.IsSuccessStatusCode) {
|
||||||
return await response.Content.ReadAsStringAsync();
|
return await response.Content.ReadAsStringAsync();
|
||||||
} else {
|
} else {
|
||||||
throw new Exception($"Erreur serveur: {response.StatusCode}");
|
string errorBody = await response.Content.ReadAsStringAsync();
|
||||||
|
throw new Exception($"Erreur serveur ({response.StatusCode}): {errorBody}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user