Exemple #1
0
        private void CheckProxy(CProxy proxy)
        {
            var before = DateTime.Now;

            try
            {
                using (var request = new HttpRequest())
                {
                    request.Proxy          = proxy.GetClient();
                    request.ConnectTimeout = (int)vm.Timeout;
                    var response = request.Get(vm.TestURL);
                    var source   = response.ToString();

                    App.Current.Dispatcher.Invoke(new Action(() => proxy.Ping = (DateTime.Now - before).Milliseconds));

                    App.Current.Dispatcher.Invoke(new Action(() => proxy.Working = source.Contains(vm.SuccessKey) ? ProxyWorking.YES : ProxyWorking.NO));

                    Globals.LogInfo(Components.ProxyManager, "Proxy '" + proxy.Proxy + $"' responded in {proxy.Ping} ms");
                }
            }
            catch (Exception ex)
            {
                Globals.LogInfo(Components.ProxyManager, "Proxy '" + proxy.Proxy + $"' failed to respond - {ex.Message}");
                App.Current.Dispatcher.Invoke(new Action(() => proxy.Working = ProxyWorking.NO));
            }

            using (var db = new LiteDatabase(Globals.dataBaseFile))
            {
                db.GetCollection <CProxy>("proxies").Update(proxy);
            }
        }
Exemple #2
0
        /// <summary>
        /// Sets a proxy to be used during the request.
        /// </summary>
        /// <param name="proxy">The proxy</param>
        /// <returns>The request itself</returns>
        public Request SetProxy(CProxy proxy)
        {
            request.Proxy = proxy.GetClient();

            request.Proxy.ReadWriteTimeout = timeout;
            request.Proxy.ConnectTimeout   = timeout;
            request.Proxy.Username         = proxy.Username;
            request.Proxy.Password         = proxy.Password;

            return(this);
        }
Exemple #3
0
        /// <summary>
        /// Downloads a remote file.
        /// </summary>
        /// <param name="fileName">The destination file on disk</param>
        /// <param name="url">The URL of the remote file</param>
        /// <param name="useProxies">Whether to use proxies for the request</param>
        /// <param name="proxy">The proxy, if needed</param>
        /// <param name="cookies">The cookies to use in the request</param>
        /// <param name="newCookies">The new cookie dictionary containing the new cookies too</param>
        /// <param name="timeout">The request timeout in milliseconds</param>
        /// <param name="userAgent">The user agent to use</param>
        public static void RemoteFile(
            string fileName, string url, bool useProxies, CProxy proxy, Dictionary <string, string> cookies, out Dictionary <string, string> newCookies, int timeout, string userAgent = "")
        {
            HttpRequest request = new HttpRequest();

            if (userAgent != string.Empty)
            {
                request.UserAgent = userAgent;
            }
            request.Cookies = new CookieDictionary();
            foreach (var cookie in cookies)
            {
                request.Cookies.Add(cookie.Key, cookie.Value);
            }

            // Set proxy
            if (useProxies)
            {
                request.Proxy = proxy.GetClient();
                request.Proxy.ReadWriteTimeout = timeout;
                request.Proxy.ConnectTimeout   = timeout;
                request.Proxy.Username         = proxy.Username;
                request.Proxy.Password         = proxy.Password;
            }

            HttpResponse response = request.Get(url);

            using (Stream inputStream = response.ToMemoryStream())
                using (Stream outputStream = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[4096];
                    int    bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);
                }

            newCookies = response.Cookies;
        }