From 64cfa39bde339b2edc17625e6aeadc8b7d252fa9 Mon Sep 17 00:00:00 2001 From: Gregor Mohorko Date: Mon, 12 Apr 2021 13:09:30 +0200 Subject: [PATCH] fix: GMHttpClient: 'Uri string is too long'. --- src/GM.Utility/GM.Utility/Net/GMHttpClient.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/GM.Utility/GM.Utility/Net/GMHttpClient.cs b/src/GM.Utility/GM.Utility/Net/GMHttpClient.cs index 9e40087..f41e090 100644 --- a/src/GM.Utility/GM.Utility/Net/GMHttpClient.cs +++ b/src/GM.Utility/GM.Utility/Net/GMHttpClient.cs @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2020 Gregor Mohorko +Copyright (c) 2021 Gregor Mohorko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -28,6 +28,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE using System; using System.Collections.Generic; +using System.Linq; +using System.Net; using System.Net.Http; using System.Text; using System.Threading; @@ -87,7 +89,16 @@ public TimeSpan Timeout public async Task UploadValuesAsync(string address, IEnumerable> nameValueCollection, System.Net.Http.HttpMethod method, CancellationToken cancellationToken) { using(var request = new HttpRequestMessage(method, address)) { - request.Content = new FormUrlEncodedContent(nameValueCollection); + { + // the constructor of FormUrlEncodedContent has a problem with large amount of data (>2000 characters) + //request.Content = new FormUrlEncodedContent(nameValueCollection); + + // therefore, let's do it ourselves: + // (idea from: https://stackoverflow.com/a/51854330/6277755) + var encodedItems = nameValueCollection + .Select(x => $"{WebUtility.UrlEncode(x.Key)}={WebUtility.UrlEncode(x.Value)}"); + request.Content = new StringContent(string.Join("&", encodedItems), null, "application/x-www-form-urlencoded"); + } HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); try {