private static long GetSizeOfResource(Uri url) { var client = new SimpleHttpGetByRangeClient(url); var response = client.Get(url, 0, 1); if (response != null) { if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location)) { if (response.Location != url.AbsoluteUri) { url = new Uri(response.Location); Console.WriteLine("Detected Redirect: " + url); GetSizeOfResource(url); } else { throw new ArgumentException("Supplied Url has no source"); } } else if (response.WasSuccessful && response.ContentRangeLength.HasValue && response.ContentRangeLength >= 0) { return(response.ContentRangeLength.Value); } else { throw new Exception("Response was not successful, status code: " + response.StatusCode); } } throw new Exception("Response was not successful, status code: unknown"); }
/// <summary> /// Returns the length of the resource by doing a quick get request /// </summary> /// <param name="uri"></param> /// <returns></returns> public static long GetContentLength(this Uri uri) { var retry = 0; var maxRetries = 5; var maxTimeOut = 600; while (retry++ <= maxRetries) { try { var client = new SimpleHttpGetByRangeClient(uri); var response = client.Get(uri, 0, 1); if (response != null) { if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location)) { if (response.Location != uri.AbsoluteUri) { uri = new Uri(response.Location); return(GetContentLength(uri)); } else { throw new ArgumentException("Supplied Url has no source"); } } else if (response.WasSuccessful && response.ContentRangeLength >= 0) { return(response.ContentRangeLength.Value); } else if (response.StatusCode == 416) //usually means zero byte file { return(response.ContentLength); } else if (response.StatusCode == 404) { // we don't retry 404 return(retry = maxRetries); } else { throw new Exception("Response was not successful, status code: " + response.StatusCode); } } } catch { if (retry > maxRetries) { throw; } var delay = (int)Math.Min(maxTimeOut, Math.Pow(retry, 5)); System.Threading.Thread.Sleep(1000 * delay); } } throw new Exception("Response was not successful"); }
/// <summary> /// Returns the length of the resource by doing a quick get request /// </summary> /// <param name="uri"></param> /// <returns></returns> public static long GetContentLength(this Uri uri) { var retry = 0; var maxRetries = 5; var maxTimeOut = 600; while (retry++ <= maxRetries) { try { var client = new SimpleHttpGetByRangeClient(uri); var response = client.Get(uri, 0, 1); if (response != null) { if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location)) { if (response.Location != uri.AbsoluteUri) { uri = new Uri(response.Location); return GetContentLength(uri); } else { throw new ArgumentException("Supplied Url has no source"); } } else if (response.WasSuccessful && response.ContentRangeLength >= 0) { return response.ContentRangeLength.Value; } else if (response.StatusCode == 416) //usually means zero byte file { return response.ContentLength; } else if (response.StatusCode == 404) { // we don't retry 404 return retry = maxRetries; } else { throw new Exception("Response was not successful, status code: " + response.StatusCode); } } } catch { if (retry > maxRetries) throw; var delay = (int) Math.Min(maxTimeOut, Math.Pow(retry, 5)); System.Threading.Thread.Sleep(1000 * delay); } } throw new Exception("Response was not successful"); }
internal void GetChunk(long start, long end, uint retryCount = 2) { using (var client = new SimpleHttpGetByRangeClient(new Uri(url))) { SimpleHttpResponse response = client.Get(start, end - start); if (response.WasSuccessful) { HandleResponse(response); } else if (retryCount > 0) { //retry GetChunk(start, end, --retryCount); } else { throw new HttpException(string.Format("Could not retrieve content from {0} between {1} and {2}", url, start, end)); } } }
private static long GetSizeOfResource(Uri url) { var client = new SimpleHttpGetByRangeClient(url); var response = client.Get(url, 0, 1); if (response != null) { if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location)) { if (response.Location != url.AbsoluteUri) { url = new Uri(response.Location); Console.WriteLine("Detected Redirect: " + url); GetSizeOfResource(url); } else { throw new ArgumentException("Supplied Url has no source"); } } else if (response.WasSuccessful && response.ContentRangeLength.HasValue && response.ContentRangeLength >= 0) { return response.ContentRangeLength.Value; } else { throw new Exception("Response was not successful, status code: " + response.StatusCode); } } throw new Exception("Response was not successful, status code: unknown"); }