Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added timeout property and implemented the property in both OkHttpNetworkHandler and NSUrlSessionHandler #201

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions src/ModernHttpClient/Android/OkHttpNetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class NativeMessageHandler : HttpClientHandler
};

public bool DisableCaching { get; set; }
public TimeSpan? Timeout { get; set; }

public NativeMessageHandler() : this(false, false) {}

Expand Down Expand Up @@ -79,6 +80,14 @@ string getHeaderSeparator(string name)

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (Timeout != null)
{
var timeout = (long)TimeOut.Value.TotalMilliseconds;
client.SetConnectTimeout(timeout, TimeUnit.Milliseconds);
client.SetWriteTimeout(timeout, TimeUnit.Milliseconds);
client.SetReadTimeout(timeout, TimeUnit.Milliseconds);
}

var java_uri = request.RequestUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
var url = new Java.Net.URL(java_uri);

Expand Down
6 changes: 6 additions & 0 deletions src/ModernHttpClient/Facades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerificati
{
}

public TimeSpan? Timeout
{
get {throw new Exception(wrongVersion);}
set {throw new Exception(wrongVersion);}
}

public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
{
throw new Exception(wrongVersion);
Expand Down
4 changes: 4 additions & 0 deletions src/ModernHttpClient/iOS/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class NativeMessageHandler : HttpClientHandler
readonly bool customSSLVerification;

public bool DisableCaching { get; set; }
public TimeSpan? Timeout { get; set; }

public NativeMessageHandler(): this(false, false) { }
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null, SslProtocol? minimumSSLProtocol = null)
Expand Down Expand Up @@ -129,6 +130,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
Url = NSUrl.FromString(request.RequestUri.AbsoluteUri),
};

if (Timeout != null)
rq.TimeoutInterval = Timeout.Value.TotalSeconds;

var op = session.CreateDataTask(rq);

cancellationToken.ThrowIfCancellationRequested();
Expand Down