1

I've reported it as a bug here: https://bugzilla.xamarin.com/show_bug.cgi?id=30604

Which was reported before here: https://bugzilla.xamarin.com/show_bug.cgi?id=22384

The problem is that if you make a Post request to a server that replies with a redirection, HttpClient will time out. This problem doesn't happen if you use MS .NET, you have to run using Mono (tested on Windows/Android/iOS)

Here is how to reproduce:

You need to have Mono and Xamarin Studio or MonoDevelop so that you can run the client console app using Mono.

Create a new ASP.NET project and add the following method to some controller (I use the default Home controller):

    [HttpPost]
    public ActionResult DeleteComment(int commentId)
    {
        return Redirect("/Home/Contact");
    }

Create a new Console project:

    var client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:55141/"); // use the correct port
    client.Timeout = TimeSpan.FromSeconds(10); // Doesn't have to be 10, just to save time. You can leave it as default

    var content1 = new StringContent("{\"commentid\":1}");
    content1.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    try {
        var r1 = client.PostAsync("/home/DeleteComment", content1).Result; // Change to your method url
        var test = r1.StatusCode;
    }
    catch (Exception e) {
        return;
    }

This will:

  • Be received and reply successfully on the server
  • Throw an exception "Task was cancelled" in the console app.

Read more:

SpaceMonkey
  • 4,143
  • 5
  • 38
  • 60

1 Answers1

0

I know, this question is pretty old, but in case someone faces this with HttpClient, this solution may help you.

HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(10);
David Oganov
  • 976
  • 1
  • 11
  • 23