Skip to content

Commit 66ef7b8

Browse files
committed
Merge pull request #1 from scoodah/master
pull
2 parents 64b36bb + 9d63e87 commit 66ef7b8

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/ModernHttpClient/Android/OkHttpNetworkHandler.cs

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class NativeMessageHandler : HttpClientHandler
2929
};
3030

3131
public bool DisableCaching { get; set; }
32+
public TimeSpan? Timeout { get; set; }
3233

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

@@ -79,6 +80,14 @@ string getHeaderSeparator(string name)
7980

8081
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
8182
{
83+
if (Timeout != null)
84+
{
85+
var timeout = (long)TimeOut.Value.TotalMilliseconds;
86+
client.SetConnectTimeout(timeout, TimeUnit.Milliseconds);
87+
client.SetWriteTimeout(timeout, TimeUnit.Milliseconds);
88+
client.SetReadTimeout(timeout, TimeUnit.Milliseconds);
89+
}
90+
8291
var java_uri = request.RequestUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
8392
var url = new Java.Net.URL(java_uri);
8493

src/ModernHttpClient/Facades.cs

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerificati
4040
{
4141
}
4242

43+
public TimeSpan? Timeout
44+
{
45+
get {throw new Exception(wrongVersion);}
46+
set {throw new Exception(wrongVersion);}
47+
}
48+
4349
public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
4450
{
4551
throw new Exception(wrongVersion);

src/ModernHttpClient/iOS/NSUrlSessionHandler.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class InflightOperation
3232
public CancellationToken CancellationToken { get; set; }
3333
public bool IsCompleted { get; set; }
3434
}
35-
35+
3636
public class NativeMessageHandler : HttpClientHandler
3737
{
3838
readonly NSUrlSession session;
@@ -52,6 +52,7 @@ public class NativeMessageHandler : HttpClientHandler
5252
readonly bool customSSLVerification;
5353

5454
public bool DisableCaching { get; set; }
55+
public TimeSpan? Timeout { get; set; }
5556

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

133+
if (Timeout != null)
134+
rq.TimeoutInterval = Timeout.Value.Seconds;
135+
132136
var op = session.CreateDataTask(rq);
133137

134138
cancellationToken.ThrowIfCancellationRequested();

0 commit comments

Comments
 (0)