Clear spinner on http request failures

- Add timeouts to RequestManager request model
 - Support timeouts in WebRequestBase<T> requests
This commit is contained in:
John Lewin 2015-12-23 13:32:30 -08:00
parent 19b7cb01cf
commit 0978570fd4
2 changed files with 17 additions and 0 deletions

View file

@ -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)

View file

@ -50,11 +50,14 @@ namespace MatterHackers.MatterControl.VersionManagement
{
protected Dictionary<string, string> requestValues;
protected string uri;
public WebRequestBase()
{
requestValues = new Dictionary<string, string>();
}
public int Timeout { get; set; } = 100000;
public event EventHandler RequestComplete;
public event EventHandler<ResponseErrorEventArgs> 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(','))));