HTTPWebRequest and after a while 400 starts (Bad Request)

I have a workflow that constantly synchronizes data from / to a remote server. When I first run the application, everything works fine. It requests a web request every 30 seconds and the data is successfully returned.

If I remain a simulator running for a long time, in the end the request will fail (400) Bad Request. And all subsequent requests do the same. If I kill the application and restart it ... everything is fine.

Does anyone have any ideas? The code is below.

public RestResponse<T> Execute<T>(RestRequest request) { var restResponse = new RestResponse<T>(); var serializer = new JavaScriptSerializer(); var urlPath = baseUrl + "/" + request.Resource; Console.WriteLine("Requesting: " + urlPath); var httpRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlPath)); httpRequest.Headers = request.Headers; foreach (string key in clientHeaders.Keys) httpRequest.Headers.Add(key, clientHeaders[key]); httpRequest.Headers.Add("Accept-Encoding", "gzip,deflate"); Authenticator.Authenticate(httpRequest); httpRequest.Method = request.Method.ToString(); HttpWebResponse httpResponse = null; try { if ((request.Method == Method.POST) && (!request.IsJsonPost)) SetPostData(httpRequest, request); if ((request.Method == Method.POST) && (request.IsJsonPost)){ SetJsonPostData(httpRequest, request); } httpResponse = (HttpWebResponse)httpRequest.GetResponse(); var reader = new StreamReader(GetStreamForResponse(httpResponse)); var responseString = reader.ReadToEnd(); Console.WriteLine(responseString); reader.Close(); restResponse.StatusCode = httpResponse.StatusCode; restResponse.Headers = httpResponse.Headers; restResponse.Data = serializer.Deserialize<T>(responseString); restResponse.ResponseStatus = ResponseStatus.Completed; httpResponse.Close(); } catch (WebException e) { restResponse.ResponseStatus = ResponseStatus.Error; restResponse.ErrorMessage = e.Message; restResponse.ErrorException = e; var webResponse = (HttpWebResponse)e.Response; if (webResponse != null) { restResponse.StatusCode = webResponse.StatusCode; restResponse.Headers = webResponse.Headers; } if (restResponse.StatusCode != HttpStatusCode.NotModified) Console.WriteLine("An exception occured:\r\n " + request.Resource + "\r\n" + e + "\r\n"); } catch (Exception ex) { restResponse.ResponseStatus = ResponseStatus.Error; restResponse.ErrorMessage = ex.Message; restResponse.ErrorException = ex; } if (httpResponse != null) httpResponse.Close(); return restResponse; } 

This does not work on this line:

 httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 

Stack trace:

 System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x002f2] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1477 at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1300 

The request never hits the remote server.

+6
source share
2 answers

The problem was in the clientHeaders collection. This RESTClient class is created once during the application life cycle. I added headers over and over and did not clear them first. After some time, the headers became too large and a bad request came up.

+2
source

I assume you are out of connections. You can try to increase them:

<configuration> <system.net> <connectionManagement> <add address="*" maxconnection="65535" /> </connectionManagement> </system.net> </configuration>

0
source

Source: https://habr.com/ru/post/917573/


All Articles