Bug: HttpClient times out when redirected on a Post request

SpaceMonkeySpaceMonkey GBMember ✭✭✭
edited May 2015 in Xamarin.Android

This has to be a bug, because it works fine on Desktop, but not on Android.
Create a new ASP.NET project and add the following method to some controller (I use the default Home controller):

        public ActionResult DeleteComment(int commentId)
            return Redirect("/Home/Contact");

Create a new Android project and add the following method:

    public void TestPostRedirect() {
        var client = new HttpClient();
        client.BaseAddress = new Uri(""); // Match this to your server address on your desktop, use this IP if you use Genymotion and change the port to the correct one
        client.Timeout = TimeSpan.FromSeconds(10);

        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) {

Run this method on Android, it will:

  • Be received successfully on the server
  • Throw an exception "Task was cancelled" on Android.

Run the exact same client code on desktop in a console app and it works perfectly fine and returns a 200 status code (OK).

For the life of me, WHY???



  • SpaceMonkeySpaceMonkey GBMember ✭✭✭
    edited May 2015

    I forgot to mention, this problem won't happen if you disable redirection using:

            var handler = new HttpClientHandler { AllowAutoRedirect = false };
            var client = new HttpClient(handler);

    It will return a status 302 (Found ) as it should

  • TedEilesTedEiles USMember ✭✭

    The happens in iOS also. Disabling redirection results in a 302 as it should too.

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    @TedEiles Thank you. Also doesn't matter whether running from a PCL assembly or a native one.

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    This is actually a bug in Mono itself, I've just tested running on desktop but using Mono instead of Microsoft .NET.
    I will file a report

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    I filed a bug report here:

    This was reported one year ago and was never fixed:

  • mapfmapf USMember
    edited June 2015

    I have the same problem. I am new to Xamarin. It seems your bug report caused a fix of the problem. It is stated to be "Fixed in master/mono 4.2"

    Does this mean it will be fixed in 4.2 when this is release? Currently they are at (beta). When it is released, how is it possible to update the xamarin projects to use the newer mono version?

    There is also the ModernHttpClient mentioned. But I don't think I can use this in Xamarin as I have a shared app between iOs/Android/Windows Phone. The ModernHttpClient only supports the first two, so I cannot reference the NativeMessageHandler from the shared project, right?

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭
    edited June 2015


    Yes, the bug was fixed. However, I ran into another bug (freeze on a simple GET), and I couldn't be bothered to report it because it would involve setting cookies and other steps to reproduce.

    Basically, stay away from HttpClient, just don't use it. What you should do is to use this:

    This doesn't have the freeze bugs. However, it gave me other problems when trying to set cookies.

    I would say:

    • If you won't be working with cookies/login then use Modern HttpClient
    • If you want a reliable solution that will also work with cookies fine, then use the native Http libraries. That's what I ended up doing, I used HttpUrlConnection (I'm on Android). Of course, the downside is having to write more code and having to use dependency injection.
  • xuan.boyxuan.boy USMember
    edited July 2015

    I have the same problem, too. But in my case, the app run well in IOS if I change PCL profile

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭


    Just give it some time into development and you may run into the second bug with GET requests. My advice is not to use HttpClient.

  • HNAHNA USMember

    The issue I am running into is that the response I get using ModernHttpClient is filtering the incoming cookies. I only get 1 cookie back where using HttpClientHandler I am getting all 4 in my case. Which will not bring anything back in the subsequent GET calls due to lack of proper header cookies. Not sure how to fix that.

  • flaviuspacsaflaviuspacsa Member

    if anyone has this issues but for a xamarin iOS application on a Mac, open the Reference.cs file. You can find it in the Web Reference folder in your project's three. Check to see if the url that you entered when you added the web reference and the one from the Reference.cs file are a match. I am referring to the url from the constructor of the Reference.cs class. Especially pay attention to the http vs https part.

Sign In or Register to comment.