From 0978570fd4fae417b9cb82f595f39b3e13db6dc1 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 23 Dec 2015 13:32:30 -0800 Subject: [PATCH] Clear spinner on http request failures - Add timeouts to RequestManager request model - Support timeouts in WebRequestBase requests --- Utilities/WebUtilities/RequestManager.cs | 11 +++++++++++ VersionManagement/WebRequestHandler.cs | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/Utilities/WebUtilities/RequestManager.cs b/Utilities/WebUtilities/RequestManager.cs index 33f5e45ea..c152db998 100644 --- a/Utilities/WebUtilities/RequestManager.cs +++ b/Utilities/WebUtilities/RequestManager.cs @@ -27,6 +27,7 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ +using Newtonsoft.Json; using System; using System.IO; using System.Net; @@ -38,6 +39,8 @@ namespace MatterHackers.MatterControl { public string LastResponse { protected set; get; } + public int Timeout { get; internal set; } = 100000; + private CookieContainer cookies = new CookieContainer(); internal string GetCookieValue(Uri SiteUri, string name) @@ -119,6 +122,9 @@ namespace MatterHackers.MatterControl HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); // Set the Method property of the request to POST. request.Method = method; + + request.Timeout = this.Timeout; + // Set cookie container to maintain cookies request.CookieContainer = cookies; request.AllowAutoRedirect = false; @@ -131,6 +137,7 @@ namespace MatterHackers.MatterControl { request.Credentials = new NetworkCredential(login, password); } + if (method == "POST") { // Convert POST data to a byte array. @@ -151,6 +158,10 @@ namespace MatterHackers.MatterControl } catch (WebException ex) { + if(ex.Status == WebExceptionStatus.Timeout) + { + LastResponse = JsonConvert.SerializeObject(new { status = "error", statuscode = 408 }); + } Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status); } catch (IOException ioException) diff --git a/VersionManagement/WebRequestHandler.cs b/VersionManagement/WebRequestHandler.cs index 02d5c1e85..2fb4dc118 100644 --- a/VersionManagement/WebRequestHandler.cs +++ b/VersionManagement/WebRequestHandler.cs @@ -50,11 +50,14 @@ namespace MatterHackers.MatterControl.VersionManagement { protected Dictionary requestValues; protected string uri; + public WebRequestBase() { requestValues = new Dictionary(); } + public int Timeout { get; set; } = 100000; + public event EventHandler RequestComplete; public event EventHandler RequestFailed; @@ -124,6 +127,9 @@ namespace MatterHackers.MatterControl.VersionManagement protected void SendRequest() { RequestManager requestManager = new RequestManager(); + + requestManager.Timeout = this.Timeout; + string jsonToSend = JsonConvert.SerializeObject(requestValues); System.Diagnostics.Trace.Write(string.Format("ServiceRequest: {0}\r\n {1}\r\n", uri, string.Join("\r\n\t", jsonToSend.Split(','))));