question

BB-9223 avatar image
BB-9223 asked ·

UWP Windows.Web.Http.HttpClient Post send Header/Content multiple times in every 4 minutes

Hi,

I'm using Windows.Web.Http.HttpClient Post to make a webservice call, the server sometimes takes about 10 minutes to prepare the response for a time-consuming process. A response returns successfully after 10 minutes on our environment. I have the HttpProgress attached to the request, when the OnProgressChanged gets fired, it always has the same following sequence for every request,

Progress: Stage SendingHeaders

Progress: Stage SendingContent

Progress: Stage SendingContent

Progress: Stage WaitingForResponse

...

wait for 10 minutes

...

Progress: Stage ReceivingHeaders

Progress: Stage ReceivingContent 0.06 of 100 MB

Progress: Stage ReceivingContent 1.06 of 100 MB
...

Progress: Stage ReceivingContent 100 of 100 MB

and then the request will be successful, and I will get a HttpResponseMessage.



When we deploy the UWP app to the users' side, most of the time the requests are successful if the process time of server is less than 4 minutes. However, when the process time of the server is up to 7-10 minutes, the HttpClient will INTERNALLY send the same Header and Content multiple times to the server every 4 minutes, so that the call gets fail finally, please note that this is under the same request and we don't have any Retry implemented.

The interesting thing is that the stage DetectingProxy that doesn't happen in our environment.

5941|2020-06-10T18:51:27|Progress: Stage DetectingProxy

5942|2020-06-10T18:51:27|Progress: Stage SendingHeaders

5943|2020-06-10T18:51:27|Progress: Stage SendingContent

5944|2020-06-10T18:51:27|Progress: Stage SendingContent

5945|2020-06-10T18:51:27|Progress: Stage WaitingForResponse

5946|2020-06-10T18:56:04|Progress: Stage DetectingProxy

5947|2020-06-10T18:56:04|Progress: Stage SendingHeaders

5948|2020-06-10T18:56:04|Progress: Stage SendingContent

5949|2020-06-10T18:56:04|Progress: Stage SendingContent

5950|2020-06-10T18:56:04|Progress: Stage WaitingForResponse

5951|2020-06-10T19:00:04|Progress: Stage DetectingProxy

5952|2020-06-10T19:00:04|Progress: Stage SendingHeaders

5953|2020-06-10T19:00:04|Progress: Stage SendingContent

5954|2020-06-10T19:00:04|Progress: Stage SendingContent

5955|2020-06-10T19:00:04P|Progress: Stage WaitingForResponse

5956|2020-06-10T19:02:13|ERROR|2|At ServerProcess:


at SharedLibrary!<BaseAddress>+0x4d7bbd

at SharedLibrary!<BaseAddress>+0x4d7b08

at SharedLibrary!<BaseAddress>+0x4d7ac7

at APP!<BaseAddress>+0x179cea5

--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at SharedLibrary!<BaseAddress>+0x4d7b99

at SharedLibrary!<BaseAddress>+0x4d7b08

at SharedLibrary!<BaseAddress>+0x4d7ac7

at APP!<BaseAddress>+0x179c543





The request should not send Headers and Contents under the same request. Please HELP! Any ideas would be appreciated!


Thank you!

windows-uwp
4 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Derive for above log we can't capture the problem, could you mind share your code for us, or check if you have set available timeout value for htttpclient. And have you add the Keep-Alive to client.DefaultRequestHeaders.Connection?

0 Votes 0 · ·

The following code looks correct, could you try to test Keep-Alive in customer environment?

0 Votes 0 · ·

Thank you so much! I'll give it a try.

0 Votes 0 · ·
Show more comments

1 Answer

BB-9223 avatar image
BB-9223 answered ·

Hi @NicoZhu-MSFT ,


Thank you so much for your reply. I haven't added the


Keep-Alive


to


client.DefaultRequestHeaders.Connection.


This is probably helpful.


Is it required for a request that has a long responding time(more than 4 minutes)? The problem is that it internally resending data to the server multiple times on users' environment, however, it doesn't happen in our environment.


Please see my code below,


class RequestTest { public async void Start() { Log.Trace("\n\nTest: Started"); try { string url = "http://server_url";

              IProgress<HttpProgress> progress = new Progress<HttpProgress>(OnProgressChanged);
              CancellationTokenSource cts = new CancellationTokenSource();

              HttpClient httpClient = new HttpClient();
              var httpRequestMsg = new HttpRequestMessage(HttpMethod.Post, new Uri(url));

              long delay = 10 * 1000 * 60;
              string data = $"name=myname&delay={delay}";

              httpRequestMsg.Method = HttpMethod.Post;
              httpRequestMsg.Content = new HttpStringContent(data);
              httpRequestMsg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");

              Log.Trace($"Delay time: {data}");

              var response = await httpClient.SendRequestAsync(httpRequestMsg).AsTask(cts.Token, progress);
              if (response.StatusCode == HttpStatusCode.Ok)
              {
                  var res = await response.Content.ReadAsStringAsync();
                  Log.Trace(res);
              }
          }
          catch (Exception ex)
          {
              Log.Error("\nException Message: \n", ex);
          }

          Log.Trace("\n\nTest: End");
      }

      private void OnProgressChanged(HttpProgress progress)
      {
          //Log progress
          //HttpClientRequest.LogHttpProgress(progress);
      }
  }


Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.