Skip to content
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