Skip to content

Commit

Permalink
fix: GMHttpClient: 'Uri string is too long'.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Apr 12, 2021
1 parent d76d039 commit 64cfa39
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/GM.Utility/GM.Utility/Net/GMHttpClient.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -87,7 +89,16 @@ public TimeSpan Timeout
public async Task<string> UploadValuesAsync(string address, IEnumerable<KeyValuePair<string, string>> 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 {
Expand Down

0 comments on commit 64cfa39

Please sign in to comment.