core 3.1 HttpClient Error while copying content to a stream.

astheras 1 Reputation point

we have old file server with post and get requests to work with files. Now we need to bind our web server with old file server. But i'm getting Error while copying content to a stream. when PostAsync. What i'm doing wrong?

    public async Task<string> UploadFile(string data) {
      dynamic execResult = null;
      TDecryptedCookie cookie = TAuthorization.GetCookieInfo(Request);
      dynamic json = TTools.Deserialize(data);

      string url = $"{TConst.FileServerHost}FileService/Upload?data={{ \"folder\":\"{json.F_Folder.Value}\", \"filename\":\"{json.F_FileName.Value}\",\"personID\":{cookie.PersonID}}}";

      dynamic result;
      var baseAddress = new Uri(url);

      try {
        using (var client = new HttpClient() { BaseAddress = baseAddress }) {
          client.DefaultRequestHeaders.Add("Authorization", Request.Headers["Authorization"].ToString()); 
          using var form = new MultipartFormDataContent();

          foreach (var file in Request.Form.Files) {
            //обработка файла
            byte[] fileBytes;

            using var ms = new MemoryStream();
            fileBytes = ms.ToArray();
            using var fileContent = new ByteArrayContent(fileBytes);
            form.Add(fileContent, "file", file.FileName);
          using var response = await client.PostAsync(baseAddress, form);   // <====== Error while copying content to a stream.
          var responseContent = await response.Content.ReadAsStringAsync();
          result = await TTools.Deserialize(responseContent);

      } catch (Exception e) {
        result = new { result = false, message = e.Message };
      return result;
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. astheras 1 Reputation point

    resolved by reading innerexception not e.Message

  2. Nijil N 1 Reputation point

    Can you pls share the innerexception and the solution.

    0 comments No comments