private async Task <string> GetRequestAsync() { const string requestUrl = "https://www.tumblr.com/"; HttpWebRequest request = webRequestFactory.CreateGetRequest(requestUrl); return(await webRequestFactory.ReadRequestToEndAsync(request)); }
public void PerformTumblrLogout() { const string url = "https://www.tumblr.com/logout"; var request = webRequestFactory.CreateGetRequest(url); cookieService.GetUriCookie(request.CookieContainer, new Uri("https://www.tumblr.com/")); using (var response = request.GetResponse() as HttpWebResponse) { cookieService.SetUriCookie(response.Cookies); } }
private async Task <string> GetUrlRedirection(string url) { HttpWebRequest request = webRequestFactory.CreateGetRequest(url); cookieService.GetUriCookie(request.CookieContainer, new Uri("https://www.tumblr.com/")); string location; using (var response = await request.GetResponseAsync().TimeoutAfter(shellService.Settings.TimeOut) as HttpWebResponse) { location = response.ResponseUri.ToString(); } return(location); }
public async Task <string> GetLatestReleaseFromServer() { version = null; downloadLink = null; try { HttpWebRequest request = webRequestFactory.CreateGetRequest("https://api.github.com/repos/tumblthreeapp/tumblthree/releases/latest"); string result = await webRequestFactory.ReadRequestToEndAsync(request); XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new XmlDictionaryReaderQuotas()); XElement root = XElement.Load(jsonReader); version = root.Element("tag_name").Value; if (Environment.Is64BitProcess) { downloadLink = root.Descendants("browser_download_url").Where(s => s.Value.Contains("x64-App")).FirstOrDefault()?.Value; } else { downloadLink = root.Descendants("browser_download_url").Where(s => s.Value.Contains("x86-App")).FirstOrDefault()?.Value; } if (string.IsNullOrEmpty(downloadLink)) { downloadLink = root.Element("assets").Element("item").Element("browser_download_url").Value; } } catch (Exception exception) { Logger.Error(exception.ToString()); return(exception.Message); } return(null); }
private async Task <string> RequestTumblrKey() { const string url = "https://www.tumblr.com/login"; var request = webRequestFactory.CreateGetRequest(url); cookieService.GetUriCookie(request.CookieContainer, new Uri("https://www.tumblr.com/")); using (var response = await request.GetResponseAsync().TimeoutAfter(shellService.Settings.TimeOut).ConfigureAwait(false) as HttpWebResponse) { cookieService.SetUriCookie(response.Cookies); using (var stream = webRequestFactory.GetStreamForApiRequest(response.GetResponseStream())) { using (var buffer = new BufferedStream(stream)) { using (var reader = new StreamReader(buffer)) { return(reader.ReadToEnd()); } } } } }
public virtual async Task <string> RequestImgurAlbumSite(string imgurAlbumUrl) { var requestRegistration = new CancellationTokenRegistration(); try { HttpWebRequest request = webRequestFactory.CreateGetRequest(imgurAlbumUrl); requestRegistration = ct.Register(() => request.Abort()); return(await webRequestFactory.ReadRequestToEndAsync(request)); } finally { requestRegistration.Dispose(); } }
public async Task <string> GetLatestReleaseFromServer() { version = null; downloadLink = null; try { HttpWebRequest request = webRequestFactory.CreateGetRequest("https://api.github.com/repos/tumblthreeapp/tumblthree/releases/latest"); string result = await webRequestFactory.ReadRequestToEndAsync(request); XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new XmlDictionaryReaderQuotas()); XElement root = XElement.Load(jsonReader); version = root.Element("tag_name").Value; downloadLink = root.Element("assets").Element("item").Element("browser_download_url").Value; } catch (Exception exception) { Logger.Error(exception.ToString()); return(exception.Message); } return(null); }
// TODO: Needs a complete rewrite. Also a append/cache function for resuming incomplete files on the disk. // Should be in separated class with support for events for downloadspeed, is resumable file?, etc. // Should check if file is complete, else it will trigger an WebException -- 416 requested range not satisfiable at every request public async Task <bool> DownloadFileWithResumeAsync(string url, string destinationPath) { long totalBytesReceived = 0; var attemptCount = 0; var bufferSize = settings.BufferSize * 4096; if (File.Exists(destinationPath)) { var fileInfo = new FileInfo(destinationPath); totalBytesReceived = fileInfo.Length; var result = await CheckDownloadSizeAsync(url, destinationPath).TimeoutAfter(settings.TimeOut); if (totalBytesReceived >= result.contentLength) { return(true); } if (destinationPath != result.destinationPath) { File.Delete(destinationPath); destinationPath = result.destinationPath; fileInfo = new FileInfo(destinationPath); totalBytesReceived = fileInfo.Length; } } if (ct.IsCancellationRequested) { return(false); } var fileMode = totalBytesReceived > 0 ? FileMode.Append : FileMode.Create; var fileStream = new FileStream(destinationPath, fileMode, FileAccess.Write, FileShare.Read, bufferSize, true); try { while (true) { attemptCount += 1; if (attemptCount > settings.MaxNumberOfRetries) { return(false); } var requestRegistration = new CancellationTokenRegistration(); try { var request = webRequestFactory.CreateGetRequest(url); requestRegistration = ct.Register(() => request.Abort()); request.AddRange(totalBytesReceived); long totalBytesToReceive = 0; bool isChunked = false; using (var response = await request.GetResponseAsync().TimeoutAfter(settings.TimeOut)) { if (url.Contains("tumblr.com") && (url.Contains(".png") || url.Contains(".pnj")) && Path.GetExtension(destinationPath).ToLower() == ".png" && (response.Headers["Content-Type"]?.Contains("jpeg") ?? false)) { fileStream.Dispose(); File.Delete(destinationPath); destinationPath = Path.Combine(Path.GetDirectoryName(destinationPath), Path.GetFileNameWithoutExtension(destinationPath) + ".jpg"); fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize, true); } isChunked = response.Headers.ToString().Contains("chunked"); totalBytesToReceive = totalBytesReceived + (response.ContentLength == -1 ? 0 : response.ContentLength); using (var responseStream = response.GetResponseStream()) using (var throttledStream = GetStreamForDownload(responseStream)) { var buffer = new byte[4096]; var bytesRead = 0; //Stopwatch sw = Stopwatch.StartNew(); while ((bytesRead = await throttledStream .ReadAsync(buffer, 0, buffer.Length, ct) .TimeoutAfter(settings.TimeOut)) > 0) { await fileStream.WriteAsync(buffer, 0, bytesRead); totalBytesReceived += bytesRead; //float currentSpeed = totalBytesReceived / (float)sw.Elapsed.TotalSeconds; //OnProgressChanged(new DownloadProgressChangedEventArgs(totalBytesReceived, // totalBytesToReceive, (long)currentSpeed)); } } isChunked = isChunked && response.Headers.ToString().Contains("Content-Range"); } if (!isChunked && totalBytesReceived >= totalBytesToReceive) { break; } if (isChunked) { attemptCount = 0; } } catch (IOException ioException) { // file in use long win32ErrorCode = ioException.HResult & 0xFFFF; if (win32ErrorCode == 0x21 || win32ErrorCode == 0x20) { return(false); } // retry (IOException: Received an unexpected EOF or 0 bytes from the transport stream) } catch (WebException webException) { if (webException.Status == WebExceptionStatus.ConnectionClosed) { // retry } else { throw; } } finally { requestRegistration.Dispose(); } } return(true); } finally { fileStream?.Dispose(); } }
// TODO: Needs a complete rewrite. Also a append/cache function for resuming incomplete files on the disk. // Should be in separated class with support for events for downloadspeed, is resumable file?, etc. // Should check if file is complete, else it will trigger an WebException -- 416 requested range not satisfiable at every request public async Task <bool> DownloadFileWithResumeAsync(string url, string destinationPath) { long totalBytesReceived = 0; var attemptCount = 0; var bufferSize = settings.BufferSize * 4096; if (File.Exists(destinationPath)) { var fileInfo = new FileInfo(destinationPath); totalBytesReceived = fileInfo.Length; if (totalBytesReceived >= await CheckDownloadSizeAsync(url).TimeoutAfter(settings.TimeOut)) { return(true); } } if (ct.IsCancellationRequested) { return(false); } var fileMode = totalBytesReceived > 0 ? FileMode.Append : FileMode.Create; using (var fileStream = new FileStream(destinationPath, fileMode, FileAccess.Write, FileShare.Read, bufferSize, true)) { while (true) { attemptCount += 1; if (attemptCount > settings.MaxNumberOfRetries) { return(false); } var requestRegistration = new CancellationTokenRegistration(); try { var request = webRequestFactory.CreateGetRequest(url); requestRegistration = ct.Register(() => request.Abort()); request.AddRange(totalBytesReceived); long totalBytesToReceive = 0; using (var response = await request.GetResponseAsync().TimeoutAfter(settings.TimeOut)) { totalBytesToReceive = totalBytesReceived + response.ContentLength; using (var responseStream = response.GetResponseStream()) { using (var throttledStream = GetStreamForDownload(responseStream)) { var buffer = new byte[4096]; var bytesRead = 0; //Stopwatch sw = Stopwatch.StartNew(); while ((bytesRead = await throttledStream .ReadAsync(buffer, 0, buffer.Length, ct) .TimeoutAfter(settings.TimeOut)) > 0) { await fileStream.WriteAsync(buffer, 0, bytesRead); totalBytesReceived += bytesRead; //float currentSpeed = totalBytesReceived / (float)sw.Elapsed.TotalSeconds; //OnProgressChanged(new DownloadProgressChangedEventArgs(totalBytesReceived, // totalBytesToReceive, (long)currentSpeed)); } } } } if (totalBytesReceived >= totalBytesToReceive) { break; } } catch (IOException ioException) { // file in use long win32ErrorCode = ioException.HResult & 0xFFFF; if (win32ErrorCode == 0x21 || win32ErrorCode == 0x20) { return(false); } // retry (IOException: Received an unexpected EOF or 0 bytes from the transport stream) } catch (WebException webException) { if (webException.Status == WebExceptionStatus.ConnectionClosed) { // retry } else { throw; } } finally { requestRegistration.Dispose(); } } return(true); } }