HttpClient.GetAsync выдает слишком большой объект запроса 413 ⇐ C#
HttpClient.GetAsync выдает слишком большой объект запроса 413
I have a .NET 8 C# Azure Functions app. It calls a third party vendor API, which returns a very large JSON. Most of the times the following code works, but only sometimes it throws 413 Request Entity Too Large error. When it throws an error in my code, the same request works perfectly fine in Postman.
public async Task GetMyLargeObjectAsync(string unique_id) { MyLargeObject myLargeObject = new MyLargeObject(); try { HttpClient client = new HttpClient() client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); client.DefaultRequestHeaders.UserAgent.ParseAdd("MyApp/1.0.0"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); var response = await client.GetAsync(_vendorEndPointURL + unique_id); response.EnsureSuccessStatusCode(); // This line throws exception when response JSON is very large if (response.Content != null) { HttpContent content = response.Content; string result = content.ReadAsStringAsync().Result; myLargeObject = JsonConvert.DeserializeObject(result); } } catch (Exception ex) { _logger.LogError(ex, $"Error getting MyLargeObject from Vendor API. Unique ID: {unique_id}"); throw; } return myLargeObject; } I even added the following in Program.cs, but still no joy:
services.Configure(options => { options.Limits.MaxRequestBodySize = long.MaxValue; options.Limits.MaxResponseBufferSize = long.MaxValue; }); services.Configure(options => { options.MaxRequestBodySize = long.MaxValue; });
Источник: https://stackoverflow.com/questions/780 ... -too-large
I have a .NET 8 C# Azure Functions app. It calls a third party vendor API, which returns a very large JSON. Most of the times the following code works, but only sometimes it throws 413 Request Entity Too Large error. When it throws an error in my code, the same request works perfectly fine in Postman.
public async Task GetMyLargeObjectAsync(string unique_id) { MyLargeObject myLargeObject = new MyLargeObject(); try { HttpClient client = new HttpClient() client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); client.DefaultRequestHeaders.UserAgent.ParseAdd("MyApp/1.0.0"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); var response = await client.GetAsync(_vendorEndPointURL + unique_id); response.EnsureSuccessStatusCode(); // This line throws exception when response JSON is very large if (response.Content != null) { HttpContent content = response.Content; string result = content.ReadAsStringAsync().Result; myLargeObject = JsonConvert.DeserializeObject(result); } } catch (Exception ex) { _logger.LogError(ex, $"Error getting MyLargeObject from Vendor API. Unique ID: {unique_id}"); throw; } return myLargeObject; } I even added the following in Program.cs, but still no joy:
services.Configure(options => { options.Limits.MaxRequestBodySize = long.MaxValue; options.Limits.MaxResponseBufferSize = long.MaxValue; }); services.Configure(options => { options.MaxRequestBodySize = long.MaxValue; });
Источник: https://stackoverflow.com/questions/780 ... -too-large
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Должен.ThrowAsync не перехватывать исключение из запроса HttpClient.GetAsync()
Anonymous » » в форуме C# - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-