Yutao Gu Yutao Gu -4 years ago 128
HTTP Question

UWP - HttpClient in while loop only executed GET once and returns the same string as the first executed value

I want to call GET method repeatedly to get random strings , as

while (!isFinished)
using (var client = new HttpClient())
string result = await client.GetStringAsync(new Uri("the consistent url"));
//I tried this as well
//var res = await client.GetAsync(new Uri("the consistent url"));
//string outp = await res.Content.ReadAsStringAsync();
return result;

It should return different strings (I'm sure) but the return values are always the same as the first execution . Then I used Fiddler to watch my network traffic , I found that there is only one GET method called and the program are still in loop and returns same value . I don't know why.

In comparison , I tried to call another API by POST method , it works well . So what's the problem?

Answer Source

You can create the HttpClient with a IHttpFilter to set up cache usage:

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
// Setting it to .NoCache always forces a new request
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.NoCache;
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;

HttpClient client = new HttpClient(filter);
// .. your request code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download