Bug 30604

Summary: HttpClient times out when redirected on a Post request
Product: [Mono] Class Libraries Reporter: Ahmed Hassan <amamh3>
Component: System.Net.HttpAssignee: Marek Safar <masafa>
Severity: normal CC: masafa, mono-bugs+mono, parmendrak
Priority: ---    
Version: unspecified   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

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.