Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#2973 from davidsh/http_timeouts
Browse files Browse the repository at this point in the history
Fix HttpClientHandler timeouts

Commit migrated from dotnet/corefx@e6bc6a3
  • Loading branch information
stephentoub committed Aug 26, 2015
2 parents d18a3e4 + 54525a2 commit 7c716f7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public HttpClientHandler()
// WPAD protocol and PAC file. So, for app-compat, we will do the same for the default proxy setting.
_winHttpHandler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy;
_winHttpHandler.Proxy = null;

// Since the granular WinHttpHandler timeout properties are not exposed via the HttpClientHandler API,
// we need to set them to infinite and allow the HttpClient.Timeout property to have precedence.
_winHttpHandler.ConnectTimeout = Timeout.InfiniteTimeSpan;
_winHttpHandler.ReceiveHeadersTimeout = Timeout.InfiniteTimeSpan;
_winHttpHandler.ReceiveDataTimeout = Timeout.InfiniteTimeSpan;
_winHttpHandler.SendTimeout = Timeout.InfiniteTimeSpan;
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Net;
using System.Net.Http;
using System.Net.Tests;
using System.Threading;
using System.Threading.Tasks;

using Xunit;
using Xunit.Abstractions;

namespace System.Net.Http.Tests
{
public class HttpClientTest
{
[Fact]
[OuterLoop]
public void Timeout_SetTo60AndGetResponseFromServerWhichTakes40_Success()
{
// TODO: This server path will change once the final test infrastructure is in place (Issue #1477).
const string SlowServer = "http://httpbin.org/drip?numbytes=1&duration=1&delay=40&code=200";

using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(60);
var response = client.GetAsync(SlowServer).GetAwaiter().GetResult();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Link>Common\System\Net\HttpTestServers.cs</Link>
</Compile>
<Compile Include="HttpClientHandlerTest.cs" />
<Compile Include="HttpClientTest.cs" />
<Compile Include="HttpMethodTest.cs" />
<Compile Include="XunitTestAssemblyAtrributes.cs" />
</ItemGroup>
Expand Down

0 comments on commit 7c716f7

Please sign in to comment.