Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:32:42+00:00 2026-06-06T13:32:42+00:00

I have a background worker thread that is constantly syncing data to/from a remote

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:32:43+00:00Added an answer on June 6, 2026 at 1:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that can spawn background worker threads from anywhere within it.
I have a small app that requires some tasks to run in the background.
I have a background worker that is started on the main thread as shown
I have a background thread that is sending data about the application's current status
Hi all i have a background worker thread and some unmanaged code dlls ,
I have a Background worker that needs to be called multiple times based on
I have a background worker that stops after 100 iterations. Like this: BackgroundWorker bgWorker
lets say i have a background worker in a class that perform db query
Scenario I have a background worker in my application that runs off and does
I have a WPF application with two background worker threads that operate while loading.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.