예제 #1
0
        private bool GetFileFromHttp(string FileUrl, string FileDisk)
        {
            var fs = new System.Net.WebClient();

            fs.Encoding = System.Text.Encoding.GetEncoding(1250);

            var CachePol = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

            fs.CachePolicy = CachePol;

            try
            {
                fs.DownloadFile(FileUrl, FileDisk);
            }
            catch (Exception ex)
            {
                if ((ex.Message ?? "") == "The remote server returned an error: (404) Not Found.")
                {
                    // MessageBox.Show("Error on Lineage II Update system. Report to support.")
                    return(false);
                }
                // UpdStatus.Text = ex.Message
                MessageBox.Show(ex.Message);
                return(false);
            }


            return(true);
        }
예제 #2
0
        /// <summary>
        /// Puts the bytes from the specified file to the API resource.
        /// </summary>
        /// <returns>
        /// The JSON response from the server in the form of an Object.
        /// </returns>
        /// <param name='path'>
        /// The path to the resource to put the bytes to.
        /// </param>
        /// <param name='parameters'>
        /// Query string parameters in the form of a Dictionary.
        /// </param>
        /// <param name='body'>
        /// A Hashtable containing the body of the request that will later be converted into JSON.
        /// </param>
        /// <param name='fileName'>
        /// The name of the file whose bytes will be sent via the PUT request.
        /// </param>
        public Object putBytes(string path, Dictionary <String, String> parameters, System.Byte[] body, String fileName = null)
        {
            var url = this.generateURL("PUT", path, parameters, Encoding.Default.GetString(body));

            System.Net.Cache.RequestCachePolicy requestCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

            request.CachePolicy = requestCachePolicy;
            request.Method      = "PUT";
            request.AllowWriteStreamBuffering = false;
            request.Timeout     = UPLOAD_TIMEOUT;
            request.SendChunked = false;

            request.ContentLength = body.Length;

            var stream = request.GetRequestStream();

            stream.Write(body, 0, body.Length);
            stream.Flush();
            stream.Close();

            if (this.getResponse(request))
            {
                return(JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd()));
            }

            return(null);
        }
        internal static CacheResponseProperty LoadFrom(System.Net.Cache.RequestCachePolicy req, HttpWebResponse response)
        {
            var property = new CacheResponseProperty()
            {
                IsFromCache = response.IsFromCache,
            };

            if (response.IsFromCache)
            {
                var policy = req as System.Net.Cache.HttpRequestCachePolicy;

                property.CacheSyncDate = policy.CacheSyncDate;
                property.Level         = policy.Level;
                property.MaxAge        = policy.MaxAge;
                property.MaxStale      = policy.MaxStale;
                property.MinFresh      = policy.MinFresh;

                property.AgeInSeconds = int.Parse(response.Headers[HttpResponseHeader.Age], CultureInfo.InvariantCulture);
                property.CacheControl = response.Headers[HttpResponseHeader.CacheControl];
                property.Vary         = response.Headers[HttpResponseHeader.Vary];
                property.Via          = response.Headers[HttpResponseHeader.Via];
                property.Warning      = response.Headers[HttpResponseHeader.Warning];
            }
            return(property);
        }
예제 #4
0
        public WebResourceFetcher(string name)
        {
            FileName = name;
            Uri      = new Uri(string.Format("{0}{1}", Settings.Default.GitUrl, FileName));

            Encoding    = Encoding.UTF8;
            CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
            Headers.Add("Cache-Control", "no-cache");
        }
예제 #5
0
        public Client(CookieContainer cookies)
        {
            if (BaseAddress == "")
            {
                BaseAddress = "https://www.howrse.de/";
            }
            CookieContainer = cookies;
            RequestUri      = new Uri(BaseAddress);
            ResponseUri     = new Uri(BaseAddress);

            Proxy = null;
            UseDefaultCredentials = true;
            CachePolicy           = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            DownloadString(BaseAddress);
        }
예제 #6
0
 // Metoda wysyłająca asynchronicznie plik na serwer FTP
 public void UploadFileAsync(string FileName)
 {
     try
     {
         System.Net.Cache.RequestCachePolicy cache = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Reload);
         WebClient client = new WebClient();
         FileInfo  file   = new FileInfo(FileName);
         Uri       uri    = new Uri((ftpDirectory + '/' + file.Name).ToString());
         client.Credentials = new NetworkCredential(this.userName, this.password);
         uploadCompleted    = false;
         if (file.Exists)
         {
             client.UploadFileCompleted   += new UploadFileCompletedEventHandler(Client_UploadFileCompleted);
             client.UploadProgressChanged += new UploadProgressChangedEventHandler(Client_UploadProgressChanged);
         }
     }
     catch
     {
         throw new Exception("Błąd: Nie można wysłać pliku");
     }
 }
예제 #7
0
        private void RunURLRequest(String cache, String ID)
        {
            String thisURL = null;

            for (int i = 0; i < cacheURLs.Length; i++)
            {
                try
                {
                    // cacheURLs[i] = Dns.GetHostName();
                    thisURL = "http://" + cacheURLs[i] + "/cache/Service?Cache=" + cache + "&ID=" + ID;
                    //URL url = new URL(thisURL);
                    Uri url = new Uri(thisURL);
                    //Url url = new Url(thisURL);
                    //Proxy thisProxy = Proxy.NO_PROXY;
                    //WebProxy thisProxy = WebProxy. GetDefaultProxy();
                    //System.Net.Cache.RequestCachePolicy v = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    ///WebRequst.DefaultCachePolicy = v;
                    //request =new we // = WebRequest.Create(url);
                    //request.CachePolicy = v;
                    WebRequest request = WebRequest.Create(url);
                    System.Net.Cache.RequestCachePolicy v = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    //WebRequst.DefaultCachePolicy = v;
                    WebRequest.DefaultCachePolicy = v;
                    WebResponse response = (WebResponse)request.GetResponse();
                    //URLConnection urlConn = url.openConnection(thisProxy);
                    //urlConn.setUseCaches(false);
                    //urlConn.connect();
                    //Reader stream = new java.io.InputStreamReader(
                    //urlConn.getInputStream());


                    Stream        stream    = response.GetResponseStream();
                    BinaryReader  reader    = new BinaryReader(stream, UTF8Encoding.UTF8);
                    StringBuilder srvOutput = new StringBuilder();

                    byte[] RecvBuffer = new byte[stream.Length];
                    int    lenth      = Convert.ToInt32(stream.Length);

                    try
                    {
                        int    c;
                        string tempstring = null;
                        while ((c = stream.Read(RecvBuffer, 0, lenth)) > 0)
                        {
                            //srvOutput.Append((char)c);
                            tempstring = Encoding.ASCII.GetString(RecvBuffer, 0, lenth);

                            // continue building the string
                            srvOutput.Append(tempstring);
                        }
                    }
                    catch (Exception E2)
                    {
                        E2.StackTrace.ToString();// .printStackTrace();
                    }
                }
                catch (Exception E)
                {
                    if (log != null)
                    {
                        log.Warning("Can't clean cache at:" + thisURL + " be carefull, your deployment server may use invalid or old cache data!" + E.Message);
                    }
                }
            }
        }
예제 #8
0
 public RestRequestBuilder WithCachePolicy(System.Net.Cache.RequestCachePolicy cachePolicy)
 {
     _request.CachePolicy = cachePolicy;
     return(this);
 }
 public BitmapImage(Uri uriSource, System.Net.Cache.RequestCachePolicy uriCachePolicy)
 {
 }
예제 #10
0
        /// <summary>
        /// Puts the bytes from the specified file to the API resource.
        /// </summary>
        /// <returns>
        /// The JSON response from the server in the form of an Object.
        /// </returns>
        /// <param name='path'>
        /// The path to the resource to put the bytes to.
        /// </param>
        /// <param name='parameters'>
        /// Query string parameters in the form of a Dictionary.
        /// </param>
        /// <param name='body'>
        /// A Hashtable containing the body of the request that will later be converted into JSON.
        /// </param>
        /// <param name='fileName'>
        /// The name of the file whose bytes will be sent via the PUT request.
        /// </param>
        public Object putBytes(string path, Dictionary<String, String> parameters, System.Byte[] body, String fileName = null)
        {
            var url = this.generateURL("PUT", path, parameters, Encoding.Default.GetString(body));

            System.Net.Cache.RequestCachePolicy requestCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.CachePolicy = requestCachePolicy;
            request.Method = "PUT";
            request.AllowWriteStreamBuffering = false;
            request.Timeout = UPLOAD_TIMEOUT;
            request.SendChunked = false;

            request.ContentLength = body.Length;

            var stream = request.GetRequestStream();
            stream.Write(body, 0, body.Length);
            stream.Flush();
            stream.Close();

            if (this.getResponse(request))
            {
                return JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }
 public static System.Windows.Media.Imaging.BitmapFrame Create(Uri bitmapUri, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, System.Net.Cache.RequestCachePolicy uriCachePolicy)
 {
     return(default(System.Windows.Media.Imaging.BitmapFrame));
 }
 public WebRequest CreateRequest(System.Uri url, WebHeaderCollection _headers, IWebProxy _proxy, int _timeout, System.Net.Cache.RequestCachePolicy _cachePolicy)
 {
     return(this.CreateRequest(url, string.Empty, _headers, _proxy, _timeout, _cachePolicy));
 }
 public WebRequest CreateRequest(System.Uri url, string _method, WebHeaderCollection _headers, IWebProxy _proxy, int _timeout, System.Net.Cache.RequestCachePolicy _cachePolicy)
 {
     return(innerWebClient.CreateRequest(url, _method, _headers, _proxy, _timeout, _cachePolicy));
 }
예제 #14
0
        /// <summary>
        /// Check if there is a file (see LastModifiedDate Header of the response) newer than the las downloaded.
        /// </summary>
        /// <param name="newUri"></param>
        /// <param name="fileNameFullPath"></param>
        /// <param name="lastModified"></param>
        /// <param name="contentLength"></param>
        /// <param name="ETag"></param>
        /// <returns></returns>
        private static bool isThereNewFileToDownload(out string fileNameFullPath, out string ETag)
        {
            // Set the currentOperation
            clsSettings.serviceSettings.currentOperation = operationList.checkingUriLastModified;

            fileNameFullPath = string.Empty;
            ETag             = string.Empty;
            DateTime lastModified = DateTime.MinValue;

            // Begining counter.
            int count = 0;
            // How many try in case of error.
            int finishAt = 10;

            // Sleep time (in minutes) for the thread in case of error.
            //TimeSpan delayByError = new TimeSpan(0, 10, 00);

            //bool result = false;
            while (count++ < finishAt)
            {
                try
                {
                    // Create a request for the URL.
                    WebRequest webRequest = WebRequest.Create(clsSettings.serviceSettings.downloadUri);

                    // Bypass the cache.
                    System.Net.Cache.RequestCachePolicy cachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    webRequest.CachePolicy = cachePolicy;

                    // Set the User-Agent Header
                    webRequest.Headers.Add(HttpRequestHeader.UserAgent.ToString(), clsSettings.serviceSettings.downloaderUserAgent);

                    // Get the response.
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    if (webRequest != null)
                    {
                        webRequest = null;                         // Cleanup
                    }
                    // Solve this, the real name of the file to download, maybe the uri in the configuration does't
                    // contain the real name of the file to download and we need it to save it to the file system.
                    System.IO.FileInfo file = new System.IO.FileInfo(webResponse.ResponseUri.AbsolutePath);
                    fileNameFullPath = string.Format("{0}{1}-{2}", clsSettings.serviceSettings.dataDirectory, clsSettings.getPrefixForFileNameFromDate(webResponse.LastModified), file.Name);
                    lastModified     = webResponse.LastModified;
                    ETag             = webResponse.Headers[HttpResponseHeader.ETag.ToString()];
                    webResponse.Close();

                    bool result = webResponse.LastModified > clsSettings.serviceSettings.downloadedFileLastModifiedDate;

                    if (webResponse != null)
                    {
                        webResponse = null;                         // Cleanup.
                    }
                    if (!result)
                    {
                        clsLogProcessing.WriteToEventLog(string.Format("La fecha del fichero a descargar es: {0}, " +
                                                                       "el sistema ha determinado que no es necesario descargarlo nuevamente.", lastModified), EventLogEntryType.Information);
                    }
                    return(result);
                }
                catch (System.Exception Ex)
                {
                    clsLogProcessing.WriteToEventLog(string.Format("Error comprobando la dirección url \"{0}\"" +
                                                                   Environment.NewLine + "Este es el intento {1} de {2} posibilidades, a intervalos de {3} " +
                                                                   "minutos. " + Environment.NewLine + "A continuación se muestran los detalles del error: " +
                                                                   Environment.NewLine + "{4}",
                                                                   clsSettings.serviceSettings.downloadUri, count, finishAt, _delayToRetryInterval.TotalMinutes, Ex.Message), EventLogEntryType.Error);
                    Thread.Sleep(_delayToRetryInterval);
                    continue;
                }
            }

            string s = clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes > 120 ? (clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes / 60).ToString() + " horas" : clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes.ToString() + " minutos";

            clsLogProcessing.WriteToEventLog(string.Format("Finalmente no se ha podido comprobar la dirección url \"{0}\"" +
                                                           Environment.NewLine + "El sistema iniciará operaciones nuevamente dentro de {1}. " +
                                                           Environment.NewLine + "Revise eventos anteriores para ver los detalles de la causa que ha impedido la operación",
                                                           clsSettings.serviceSettings.downloadUri, s), EventLogEntryType.Error);
            return(false);
        }
예제 #15
0
        /// <summary>
        /// Download the source file containing the list of categories and blacklisted sites.
        /// </summary>
        /// <param name="realUri"></param>
        /// <param name="finalFileName"></param>
        private static bool downloadSourceFile(string fileNameFullPath, string originalETag)
        {
            // Set the currentOperation
            clsSettings.serviceSettings.currentOperation = operationList.downloadingSourceFile;

            // Begining counter.
            int count = 0;
            // How many try in case of error.
            int finishAt = 20;
            // Used to add range to resume the download if needed.
            long startingPoint = 0;

            //bool result = false;
            while (count++ < finishAt)
            {
                try
                {
doResume:           // If we get here then the download was interrupted without report any error.
                    HttpWebRequest webRequest = null;
                    HttpWebResponse webResponse  = null;
                    Stream          strResponse  = null;
                    Stream          strLocalFile = null;
                    long            contentLength;
                    DateTime        lastModified;

beginAgain:         // If we get here, probably the file in the server was changed in the middle of the doanloading process.
                    if (File.Exists(fileNameFullPath))
                    {
                        startingPoint = new FileInfo(fileNameFullPath).Length;
                    }
                    else
                    {
                        startingPoint = 0;
                    }

                    webRequest = (HttpWebRequest)WebRequest.Create(clsSettings.serviceSettings.downloadUri);
                    webRequest.Headers.Add(HttpRequestHeader.UserAgent.ToString(), clsSettings.serviceSettings.downloaderUserAgent);
                    System.Net.Cache.RequestCachePolicy cachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    webRequest.CachePolicy = cachePolicy;
                    webRequest.AddRange(Convert.ToInt32(startingPoint));
                    webRequest.Timeout          = 180000;            // Three minutes before the request timeout.
                    webRequest.ReadWriteTimeout = 600000;            // Ten minutes for stream (Read, Write) operations.

                    webResponse = (HttpWebResponse)webRequest.GetResponse();

                    contentLength = webResponse.ContentLength + startingPoint;
                    lastModified  = webResponse.LastModified;

                    // Check if the file to download is the same checked by the "isThereNewFileToDownload" method.
                    if (originalETag != webResponse.Headers[HttpResponseHeader.ETag.ToString()])
                    {
                        // Delete the old file we were downloading because it was changed in the middle of the process and there is a new one to download.
                        File.Delete(fileNameFullPath);
                        // Get again the correct "fileNameFullPath" and "ETag" values of the new file to download.
                        isThereNewFileToDownload(out fileNameFullPath, out originalETag);
                        // Set the currentOperation to "downloadingSourceFile" again because the "isThereNewFileToDownload" method had chenged it.
                        clsSettings.serviceSettings.currentOperation = operationList.downloadingSourceFile;
                        //cleanup
                        webResponse.Close();
                        // Go to the begining of the process again.
                        goto beginAgain;
                    }

                    strResponse = webResponse.GetResponseStream();

                    if (startingPoint == 0)
                    {
                        strLocalFile = new FileStream(fileNameFullPath, FileMode.Create, FileAccess.Write, FileShare.None);
                    }
                    else
                    {
                        strLocalFile = new FileStream(fileNameFullPath, FileMode.Append, FileAccess.Write, FileShare.None);
                    }

                    // Stores the current number of bytes retrieved from the server.
                    int byteSize = 0;
                    // A buffer to store and write the data retrieved from server.
                    byte[] downBuffer = new byte[2048];

                    while ((byteSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                    {
                        strLocalFile.Write(downBuffer, 0, byteSize);
                    }

                    // Do some cleanup here.
                    if (webRequest != null)
                    {
                        webRequest = null;
                    }
                    if (webResponse != null)
                    {
                        webResponse.Close();
                        webResponse = null;
                    }
                    if (strResponse != null)
                    {
                        strResponse.Close();
                        strResponse.Dispose();
                    }
                    if (strLocalFile != null)
                    {
                        strLocalFile.Close();
                        strLocalFile.Dispose();
                    }

                    System.IO.FileInfo file = new System.IO.FileInfo(fileNameFullPath);
                    if (file.Length == contentLength)
                    {
                        clsSettings.serviceSettings.downloadedFileName             = new FileInfo(fileNameFullPath).Name;
                        clsSettings.serviceSettings.downloadedFileLastModifiedDate = lastModified;
                        clsSettings.serviceSettings.downloadedFileETag             = originalETag;
                        return(true);
                    }
                    else
                    {
                        goto doResume;
                    }
                }
                catch (System.Exception Ex)
                {
                    if (startingPoint != 0 & File.Exists(fileNameFullPath) & Ex.Message.Contains("Requested Range Not Satisfiable"))                    // Probably (416) Error - RequestedRangeNotSatisfiable Exception.
                    {
                        File.Delete(fileNameFullPath);
                    }
                    clsLogProcessing.WriteToEventLog(string.Format("Error descargando desde dirección url \"{0}\"" +
                                                                   Environment.NewLine + "Este es el intento {1} de {2} posibilidades, a intervalos de {3} " +
                                                                   "minutos. " + Environment.NewLine + "A continuación se muestran los detalles del error: " +
                                                                   Environment.NewLine + "{4}",
                                                                   clsSettings.serviceSettings.downloadUri, count, finishAt, _delayToRetryInterval.TotalMinutes, Ex.Message), EventLogEntryType.Error);
                    Thread.Sleep(_delayToRetryInterval);
                    continue;
                }
            }

            string s = clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes > 120 ? (clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes / 60).ToString() + " horas" : clsSettings.serviceSettings.checkForUpdateInterval.TotalMinutes.ToString() + " minutos";

            clsLogProcessing.WriteToEventLog(string.Format("Finalmente no se ha podido descargar desde dirección url \"{0}\"" +
                                                           Environment.NewLine + "El sistema iniciará operaciones nuevamente dentro de {1}. " +
                                                           Environment.NewLine + "Revise eventos anteriores para ver los detalles de la causa que ha impedido la operación.",
                                                           clsSettings.serviceSettings.downloadUri, s), EventLogEntryType.Error);
            return(false);
        }