Window service failing with error The remote server returned an error: (400) Bad Request.

Posted by Neerajsinghmail under ASP.NET on 12/23/2015 | Points: 10 | Views : 419 | Status : [Member] | Replies : 1
Hello Team,

I have a window service in which I am consuming a 3rd party rest service and based on response inserting that data in our SQL database.

Window service is running continuously on the system and based on a timer invoking a method.

Its working fine but failing in one scenario when my consumed 3rd party rest service goes down then its giving below error message -

The remote server returned an error: (400) Bad Request.

After this error message window service scheduling its next run after 1 hrs and it again giving the same above error message while 3rd party service had been up and running fine. In every next run its giving similar error message.

To resolve the issue I stopped the window service and started again through service.msc console. After every restart of service its working fine but when its calling similar method via timer then its not working.

I am using below code -

// Call 3rd party service
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", authCode);
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();

using (StreamReader sr = new StreamReader(response.GetResponseStream()))

I did some google and find that we also need to close the response and Abort the request but I am not sure If this could resolve the problem..

Thanks in advance for your response.


Posted by: Sheonarayan on: 12/23/2015 [Administrator] HonoraryPlatinum | Points: 25

I think the suggestion given after google search is just fine. When you do not close the response and abort the request, the default http caching may be working for you that is giving the response back without even checking whether the service has really started.

So do what is suggested, if every catch block close all the service request and related object and after 1 hour re-create the request object to request the service.

This should work.


Sheo Narayan

Neerajsinghmail, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response