I have a background worker thread that is constantly syncing data to/from a remote server. When I first run the app, everything appears to work fine. It makes the web request about every 30 seconds and the data returns successfully.
If I leave the simulator running for a long time, eventually the request will fail with a (400) Bad Request. And all subsequent requests do the same thing. If I kill the app and restart it...all is well.
Anyone have any ideas? 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;
}
It fails 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 is never getting to the remote server.
The problem was in the clientHeaders
collection. This RESTClient
class is instantiated once during the applicaiton life cycle. I was adding headers over and over and not clearing them out first. After a while, the headers got too big and a bad request was issued.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With