Bug 30604 - HttpClient times out when redirected on a Post request
Summary: HttpClient times out when redirected on a Post request
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2015-05-29 16:26 UTC by Ahmed Hassan
Modified: 2015-07-02 15:06 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Ahmed Hassan 2015-05-29 16:26:07 UTC
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) {

On Mono (Windows/iOS/Android) this throws exception:
System.Threading.Tasks.TaskCanceledException: A task was canceled.

On .Net it doesn't and returns status code OK as it should.

This is reproducible by others.
Read more here:
Comment 1 Ahmed Hassan 2015-05-29 16:27:22 UTC
Sorry, I typed "Android project" by mistake. You can create a normal console project on Windows, just make sure to run it with Mono.
You could also create an Android/iOS project, it has the same bug. It's a problem with Mono itself.
Comment 2 Marek Safar 2015-06-04 11:12:11 UTC
I cannot reproduce it locally. 

How do I setup to match to what you have as close as possible?
Comment 3 Ahmed Hassan 2015-06-05 08:11:17 UTC
Here are the two projects:

Here is the complete error message when the console test with Mono 4.0.1:
System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
  --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task.ThrowIfExceptional (Boolean includeTaskCanceledExceptions) [0x00014] in C:\buildroot\release\repos\mono\external\referencesource\mscorlib\system\threading\Tasks\Task.cs:2168 
  at System.Threading.Tasks.Task`1[System.Net.Http.HttpResponseMessage].GetResultCore (Boolean waitCompletionNotification) [0x00034] in C:\buildroot\release\repos\mono\external\referencesource\mscorlib\system\threading\Tasks\Future.cs:568 
  at System.Threading.Tasks.Task`1[System.Net.Http.HttpResponseMessage].get_Result () [0x00000] in C:\buildroot\release\repos\mono\external\referencesource\mscorlib\system\threading\Tasks\Future.cs:535 
  at HttpClientTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---

It's very simple really. I can also send you my workaround if you are interested; I basically made a custom handler and I handle redirects manually there.
Thanks for taking the time :)
Comment 4 Marek Safar 2015-06-05 13:26:35 UTC
I still cannot reproduce it, your web test server does not really run on mono
Comment 5 Ahmed Hassan 2015-06-05 14:10:16 UTC
No it doesn't.

The console app runs on Mono, the server runs on .NET
Comment 6 Ahmed Hassan 2015-06-06 11:23:05 UTC
You still can't reproduce?

I've tested this on two different machines, both running Windows and on Android emulators (Xamarin Android). It has been reproduced by another member of the forums on iOS too (check the link I posted earlier for the thread).

It doesn't matter where the server is running, I tested against local server and remote ones that I don't own, but have the source code for, so I can see they are doing the same redirect on post request.
Comment 7 Ahmed Hassan 2015-06-09 09:34:37 UTC
Btw, the problem is in HttpClientHandler.
Another proof is that when I use ModernHttpClient, which is a drop-in replacement for HttpClientHandler, the bug doesn't happen anymore:
Comment 8 Marek Safar 2015-06-11 05:31:00 UTC
Fixed in master/mono 4.2
Comment 9 Ahmed Hassan 2015-06-12 16:29:22 UTC
By the gods ! I can't believe. This is the first bug ever that I report, and it gets fixed. I'm blessed.
Comment 10 Parmendra Kumar 2015-07-02 15:06:20 UTC
To verifying this issue, I have just tried to reproduce this issue. But the test project link in comment 3 is not available now. 

I have tried to reproduce it by creating a project using code given in bug description :

Project: https://www.dropbox.com/s/rmu4vg10ynuv97x/Project_30604.zip?dl=0

@Ahmed It would be great if you provide the test project, so that we can reproduce this issue at our end and verify it efficiently.