From 11bde3978dfbfbe440801e4dfca099e523c2844e Mon Sep 17 00:00:00 2001 From: Mikhail Swift Date: Thu, 18 Feb 2016 14:22:30 -0500 Subject: [PATCH 1/6] added timeout property and implemented the property in both OkHttoNetworkHandler and NSUrlSessionHandler --- src/ModernHttpClient/Android/OkHttpNetworkHandler.cs | 9 +++++++++ src/ModernHttpClient/Facades.cs | 6 ++++++ src/ModernHttpClient/iOS/NSUrlSessionHandler.cs | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 6839410..2a1279f 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -29,6 +29,7 @@ public class NativeMessageHandler : HttpClientHandler }; public bool DisableCaching { get; set; } + public TimeSpan? Timeout { get; set; } public NativeMessageHandler() : this(false, false) {} @@ -79,6 +80,14 @@ string getHeaderSeparator(string name) protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + if (Timeout != null) + { + var timeout = (long)TimeOut.Value.TotalMilliseconds; + client.SetConnectTimeout((long)timeout, TimeUnit.Milliseconds); + client.SetWriteTimeout((long)timeout, TimeUnit.Milliseconds); + client.SetReadTimeout((long)timeout, TimeUnit.Milliseconds); + } + var java_uri = request.RequestUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped); var url = new Java.Net.URL(java_uri); diff --git a/src/ModernHttpClient/Facades.cs b/src/ModernHttpClient/Facades.cs index 114b221..541816b 100644 --- a/src/ModernHttpClient/Facades.cs +++ b/src/ModernHttpClient/Facades.cs @@ -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); diff --git a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs index 610999d..cda825a 100644 --- a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs +++ b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs @@ -32,7 +32,7 @@ class InflightOperation public CancellationToken CancellationToken { get; set; } public bool IsCompleted { get; set; } } - + public class NativeMessageHandler : HttpClientHandler { readonly NSUrlSession session; @@ -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) @@ -129,6 +130,9 @@ protected override async Task SendAsync(HttpRequestMessage Url = NSUrl.FromString(request.RequestUri.AbsoluteUri), }; + if (Timeout != null) + rq.TimeoutInterval = Timeout.Value.Seconds; + var op = session.CreateDataTask(rq); cancellationToken.ThrowIfCancellationRequested(); From 433fb1e4eebeeee3065046fc13cde3e58893cbea Mon Sep 17 00:00:00 2001 From: Mikhail Swift Date: Thu, 18 Feb 2016 14:28:10 -0500 Subject: [PATCH 2/6] removed unnecessary casts to long on the timeout property in OkHttpNetworkHandler --- src/ModernHttpClient/Android/OkHttpNetworkHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs index 2a1279f..decc548 100644 --- a/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs @@ -83,9 +83,9 @@ protected override async Task SendAsync(HttpRequestMessage if (Timeout != null) { var timeout = (long)TimeOut.Value.TotalMilliseconds; - client.SetConnectTimeout((long)timeout, TimeUnit.Milliseconds); - client.SetWriteTimeout((long)timeout, TimeUnit.Milliseconds); - client.SetReadTimeout((long)timeout, TimeUnit.Milliseconds); + 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); From 9d63e87c951fa03569a348631614866980589b4b Mon Sep 17 00:00:00 2001 From: Mikhail Swift Date: Thu, 18 Feb 2016 15:53:01 -0500 Subject: [PATCH 3/6] removed space in setter of Timeout property --- src/ModernHttpClient/Facades.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModernHttpClient/Facades.cs b/src/ModernHttpClient/Facades.cs index 541816b..16bcb12 100644 --- a/src/ModernHttpClient/Facades.cs +++ b/src/ModernHttpClient/Facades.cs @@ -43,7 +43,7 @@ public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerificati public TimeSpan? Timeout { get {throw new Exception(wrongVersion);} - set { throw new Exception(wrongVersion);} + set {throw new Exception(wrongVersion);} } public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback) From e1d08a936c1bba0f4fa68fe71a8163ed5d83f851 Mon Sep 17 00:00:00 2001 From: xk0gap Date: Thu, 1 Mar 2018 19:57:35 -0500 Subject: [PATCH 4/6] NSUrlSessionHandler: TimeoutInterval to TotalSeconds rather than Seconds The setting of: rq.TimeoutInterval = Timeout.Value.Seconds; Should be: rq.TimeoutInterval = Timeout.Value.TotalSeconds; so that the TimeoutInterval is set to the TimeSpan in seconds rather than the seconds part of the Timespan which would be zero for any whole minute. --- src/ModernHttpClient/iOS/NSUrlSessionHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs index cda825a..c25ede3 100644 --- a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs +++ b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs @@ -131,7 +131,7 @@ protected override async Task SendAsync(HttpRequestMessage }; if (Timeout != null) - rq.TimeoutInterval = Timeout.Value.Seconds; + rq.TimeoutInterval = Timeout.Value.TotalSeconds; var op = session.CreateDataTask(rq); From a3d6cf5675943289fcec9b9a64740679e971d6bd Mon Sep 17 00:00:00 2001 From: Mikhail Swift Date: Mon, 5 Mar 2018 13:25:43 -0500 Subject: [PATCH 5/6] Use TotalSeconds to set android timeout --- src/ModernHttpClient/iOS/NSUrlSessionHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs index cda825a..c25ede3 100644 --- a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs +++ b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs @@ -131,7 +131,7 @@ protected override async Task SendAsync(HttpRequestMessage }; if (Timeout != null) - rq.TimeoutInterval = Timeout.Value.Seconds; + rq.TimeoutInterval = Timeout.Value.TotalSeconds; var op = session.CreateDataTask(rq); From c19cc6a0f9b5575a8a831efde5c2301562863039 Mon Sep 17 00:00:00 2001 From: Mikhail Swift Date: Mon, 5 Mar 2018 13:26:12 -0500 Subject: [PATCH 6/6] Remove extraneous spaces --- src/ModernHttpClient/iOS/NSUrlSessionHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs index c25ede3..be4e248 100644 --- a/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs +++ b/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs @@ -32,7 +32,7 @@ class InflightOperation public CancellationToken CancellationToken { get; set; } public bool IsCompleted { get; set; } } - + public class NativeMessageHandler : HttpClientHandler { readonly NSUrlSession session;