Bug: HttpClient times out when redirected on a Post request

SpaceMonkeySpaceMonkey Ahmed HassanGBMember ✭✭✭
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):

        [HttpPost]
        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("http://10.0.3.2:55141/"); // 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) {
            return;
        }
    }

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???

Tagged:

Answers

  • SpaceMonkeySpaceMonkey Ahmed Hassan 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 Ted Eiles USMember ✭✭

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

  • SpaceMonkeySpaceMonkey Ahmed Hassan GBMember ✭✭✭

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

  • SpaceMonkeySpaceMonkey Ahmed Hassan 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 Ahmed Hassan GBMember ✭✭✭

    I filed a bug report here:
    https://bugzilla.xamarin.com/show_bug.cgi?id=30604

    This was reported one year ago and was never fixed:
    https://bugzilla.xamarin.com/show_bug.cgi?id=22384

  • mapfmapf Markus Pfleger 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 4.0.2.5 (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 Ahmed Hassan GBMember ✭✭✭
    edited June 2015

    @mapf

    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:
    https://components.xamarin.com/view/modernhttpclient

    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 san kira 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 Ahmed Hassan GBMember ✭✭✭

    @xuan.boy

    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 Hussain Abbasi 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.

Sign In or Register to comment.