How do I send large data files using HttpClient.Po

2020-05-04 05:12发布

问题:

My function is below. It works just great with anything up to around 25MB, but greater than that and it stops working. When I say stops working, it throws no exceptions and fails to the noserver result option at the bottom of the function. I can't seem to find any settings referring to any other buffer size though I found some Microsoft Documentation which does say the response buffer is 2GB default.

Any ideas, any help would be appreciated.

  • VS2019 ASP.NET
  • 4.7.2
  • Winforms

    public async Task<JsonApiResult> GetHttpData(string Message, string serviceMethod)
    {
        System.Diagnostics.Debug.Print($"{serviceMethod}{Message}");
    
        HttpClient _httpClient = new HttpClient();
    
        _httpClient.DefaultRequestHeaders.Accept.Clear();
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/text"));
    
        _httpClient.BaseAddress = new Uri(_url);
    
        try
        {
            //////
            StringContent payload = new StringContent(Message, Encoding.UTF8, "application/json");
    
            HttpResponseMessage getResponse = await _httpClient.PostAsync(serviceMethod, payload);
    
            if (getResponse.IsSuccessStatusCode)
            {
                try
                {
                    var response = getResponse.Content.ReadAsStringAsync().Result;
    
                    JsonApiResult result = JsonConvert.DeserializeObject<JsonApiResult>(response);
    
                    return result;
                }
                catch (Exception e)
                {
                    return new JsonApiResult { Result = "fail", Message = e.Message, Data = "" };
                }
            }
            else
            {
                //<<<<<<<<<<<<<<<<<<<<<<<<<
                return new JsonApiResult { Result = "fail", Message = getResponse.StatusCode.ToString(), Data = "" };
            }
        }
        catch (Exception e)
        {
            return new JsonApiResult { Result = "errserver", Message = e.Message, Data = "" };
        }
    
        return new JsonApiResult { Result = "noserver", Message = "Could not connect to server", Data = "" };
    }
    

On the server-side I'm using

    public async Task<JsonApiResult> GetMyFileUploads([FromBody]JsonApiResult result)
    {
    ...