简体   繁体   中英

C# HttpClient Bad Request on second call

I've found with the below code that on any second httpclient request, I always get a 400 Bad Request.

I can confirm the API calls actually work and return a 200 - so if I flip them, the first call will return 200 second call, 400.

I can't figure out why this is the case.

 using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(BaseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage blah = await client.GetAsync("api/subscriptions?account=eclipse&application=Sample");
            HttpResponseMessage dfds = await client.GetAsync("api/subscriptions?account=eclipse");
...
}

It's hard to tell without seeing the controller function you're trying to call. What's probably happening is that the REST service is experiencing an exception on the server and is throwing back a 400.

What web server is the service using? Maybe it has some sort of simultaneous request blocking. You could try not using async.

What I would do is open up Fiddler ( http://www.telerik.com/fiddler ) and look at the network traffic. Look at the response from the web server and see if there is any exception data there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM