Example #1
0
 /// <summary>
 /// Donwload a file from given <paramref name="url"/> and save it to <paramref name="downloadFile"/>.
 /// </summary>
 /// <param name="url">Url to download</param>
 /// <param name="downloadFile">Target file name</param>
 /// <returns><c>true</c> if successful</returns>
 public bool DownloadFile(string url, string downloadFile)
 {
     if (File.Exists(downloadFile))
     {
         return(true);
     }
     try
     {
         try
         {
             _fileLock.EnterWriteLock();
             if (File.Exists(downloadFile))
             {
                 return(true);
             }
             using (WebClient webClient = new CompressionWebClient())
                 webClient.DownloadFile(url, downloadFile);
             return(true);
         }
         catch (Exception ex)
         {
             ServiceRegistration.Get <ILogger>().Warn("OnlineLibraries.Downloader: Exception when downloading file {0} from {1} ({2})", downloadFile, url, ex.Message);
             return(false);
         }
     }
     finally
     {
         _fileLock.ExitWriteLock();
     }
 }
Example #2
0
        /// <summary>
        /// Downloads the JSON string from API.
        /// </summary>
        /// <param name="url">Url to download</param>
        /// <returns>JSON result</returns>
        protected string DownloadJSON(string url)
        {
            CompressionWebClient webClient = new CompressionWebClient(EnableCompression)
            {
                Encoding = Encoding.UTF8
            };

            foreach (KeyValuePair <string, string> headerEntry in Headers)
            {
                webClient.Headers[headerEntry.Key] = headerEntry.Value;
            }

            return(webClient.DownloadString(url));
        }
Example #3
0
        public async Task <string> DownloadStringAsync(string url)
        {
            using (CompressionWebClient webClient = new CompressionWebClient(EnableCompression)
            {
                Encoding = Encoding.UTF8
            })
            {
                foreach (KeyValuePair <string, string> headerEntry in Headers)
                {
                    webClient.Headers[headerEntry.Key] = headerEntry.Value;
                }

                return(await webClient.DownloadStringTaskAsync(url).ConfigureAwait(false));
            }
        }
    protected override string DownloadJSON(string url)
    {
      var webClient = new CompressionWebClient(EnableCompression) { Encoding = Encoding.UTF8 };
      foreach (var headerEntry in Headers)
        webClient.Headers[headerEntry.Key] = headerEntry.Value;

      LIMITER.RateLimit().Wait();
      try
      {
        return webClient.DownloadString(url);
      }
      finally
      {
        LIMITER.RequestDone();
      }
    }
Example #5
0
        /// <summary>
        /// Donwload a file from given <paramref name="url"/> and save it to <paramref name="downloadFile"/>.
        /// </summary>
        /// <param name="url">Url to download</param>
        /// <param name="downloadFile">Target file name</param>
        /// <returns><c>true</c> if successful</returns>
        public async Task <bool> DownloadFileAsync(string url, string downloadFile)
        {
            if (string.IsNullOrEmpty(downloadFile))
            {
                return(false);
            }
            if (File.Exists(downloadFile))
            {
                return(true);
            }

            using (await _fileLock.WriterLockAsync(downloadFile).ConfigureAwait(false))
            {
                try
                {
                    if (File.Exists(downloadFile))
                    {
                        return(true);
                    }
                    byte[] data = null;
                    using (WebClient webClient = new CompressionWebClient())
                    {
                        foreach (KeyValuePair <string, string> headerEntry in Headers)
                        {
                            webClient.Headers[headerEntry.Key] = headerEntry.Value;
                        }

                        data = await webClient.DownloadDataTaskAsync(url).ConfigureAwait(false);
                    }
                    if (data?.LongLength > 0)
                    {
                        using (FileStream sourceStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
                            await sourceStream.WriteAsync(data, 0, data.Length);
                        return(true);
                    }
                    ServiceRegistration.Get <ILogger>().Warn("OnlineLibraries.Downloader: No data received when downloading file {0} from {1}", downloadFile, url);
                    return(false);
                }
                catch (Exception ex)
                {
                    ServiceRegistration.Get <ILogger>().Warn("OnlineLibraries.Downloader: Exception when downloading file {0} from {1} ({2})", downloadFile, url, ex.Message);
                    return(false);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Donwload a file from given <paramref name="url"/> and save it to <paramref name="downloadFile"/>.
 /// </summary>
 /// <param name="url">Url to download</param>
 /// <param name="downloadFile">Target file name</param>
 /// <returns><c>true</c> if successful</returns>
 public bool DownloadFile(string url, string downloadFile)
 {
     if (File.Exists(downloadFile))
     {
         return(true);
     }
     try
     {
         WebClient webClient = new CompressionWebClient();
         webClient.DownloadFile(url, downloadFile);
         return(true);
     }
     catch (Exception ex)
     {
         // TODO: logging
         return(false);
     }
 }
Example #7
0
 /// <summary>
 /// Loads the image from the given path
 /// </summary>
 /// <param name="path">Path of image that should be used for this banner</param>
 /// <returns>True if successful, false otherwise</returns>
 protected Image LoadImage(String path)
 {
   try
   {
     WebClient client = new CompressionWebClient();
     byte[] imgData = client.DownloadData(path);
     MemoryStream ms = new MemoryStream(imgData);
     Image img = Image.FromStream(ms, true, true);
     return img;
   }
   catch (Exception ex)
   {
     Log.Error("Error while loading image ", ex);
     return null;
   }
 }
Example #8
0
 protected byte[] DownloadData(string url)
 {
   using (WebClient webClient = new CompressionWebClient { Encoding = Encoding.UTF8 })
     return webClient.DownloadData(url);
 }
Example #9
0
 protected string DownloadString(string url)
 {
   using (WebClient webClient = new CompressionWebClient { Encoding = Encoding.UTF8 })
     return webClient.DownloadString(url);
 }
Example #10
0
    /// <summary>
    /// Communicates to and from Trakt
    /// </summary>
    /// <param name="address">The URI to use</param>
    /// <param name="data">The Data to send</param>
    /// <returns>The response from Trakt</returns>
    private static string Transmit(string address, string data)
    {
      if (OnDataSend != null) OnDataSend(address, data);

      try
      {
        ServicePointManager.Expect100Continue = false;
        WebClient client = new CompressionWebClient(true) { Encoding = Encoding.UTF8 };
        client.Headers.Add("user-agent", UserAgent);

        // wait for a response from the server
        string response = client.UploadString(address, data);

        // received data, pass it back
        if (OnDataReceived != null) OnDataReceived(response);
        return response;
      }
      catch (WebException e)
      {
        if (OnDataError != null) OnDataError(e.Message);

        if (e.Status == WebExceptionStatus.ProtocolError)
        {
          var response = ((HttpWebResponse)e.Response);
          try
          {
            using (var stream = response.GetResponseStream())
            {
              using (var reader = new StreamReader(stream))
              {
                return reader.ReadToEnd();
              }
            }
          }
          catch { }
        }

        // create a proper response object
        TraktResponse error = new TraktResponse
        {
          Status = "failure",
          Error = e.Message
        };
        return error.ToJSON();
      }
    }