Exemple #1
0
            private void prepareLocalCache()
            {
                string cacheFilePath           = storage.GetFullPath(cache_database_name);
                string compressedCacheFilePath = $"{cacheFilePath}.bz2";

                cacheDownloadRequest = new FileWebRequest(compressedCacheFilePath, $"https://assets.ppy.sh/client-resources/{cache_database_name}.bz2");

                cacheDownloadRequest.Failed += ex =>
                {
                    File.Delete(compressedCacheFilePath);
                    File.Delete(cacheFilePath);

                    Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache download failed: {ex}", LoggingTarget.Database);
                };

                cacheDownloadRequest.Finished += () =>
                {
                    try
                    {
                        using (var stream = File.OpenRead(cacheDownloadRequest.Filename))
                            using (var outStream = File.OpenWrite(cacheFilePath))
                                using (var bz2 = new BZip2Stream(stream, CompressionMode.Decompress, false))
                                    bz2.CopyTo(outStream);

                        // set to null on completion to allow lookups to begin using the new source
                        cacheDownloadRequest = null;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache extraction failed: {ex}", LoggingTarget.Database);
                        File.Delete(cacheFilePath);
                    }
                    finally
                    {
                        File.Delete(compressedCacheFilePath);
                    }
                };

                cacheDownloadRequest.PerformAsync();
            }
Exemple #2
0
        public (FileInfo, string) Download(ChildrenBeatmap beatmap)
        {
            var req = new FileWebRequest(
                _tmpStorage.GetFullPath(beatmap.BeatmapId.ToString(), true),
                $"https://osu.ppy.sh/osu/{beatmap.BeatmapId}");

            _limiter.Limit();

            req.Perform();

            string   fileMd5;
            FileInfo info;

            using (var f = _tmpStorage.GetStream(beatmap.BeatmapId.ToString(), FileAccess.Read, FileMode.Open))
            {
                using var db = _cache.GetForWrite();
                info         = _store.Add(f);

                fileMd5 = f.ComputeMD5Hash();
                if (db.Context.CacheBeatmaps.Any(bm => bm.BeatmapId == beatmap.BeatmapId))
                {
                    db.Context.CacheBeatmaps.Update(new Beatmap
                    {
                        BeatmapId = beatmap.BeatmapId, Hash = info.Hash, FileMd5 = fileMd5
                    });
                }
                else
                {
                    db.Context.CacheBeatmaps.Add(new Beatmap
                    {
                        BeatmapId = beatmap.BeatmapId, Hash = info.Hash, FileMd5 = fileMd5
                    });
                }
            }

            _tmpStorage.Delete(beatmap.BeatmapId.ToString());

            return(info, fileMd5);
        }
Exemple #3
0
        public void Deserialize()
        {
            MemoryStream ms = new MemoryStream();

            ms.Write(_serialized, 0, _serialized.Length);
            ms.Position = 0;

            BinaryFormatter bf  = new BinaryFormatter();
            FileWebRequest  req = (FileWebRequest)bf.Deserialize(ms);

            Assert.AreEqual("CGN", req.ConnectionGroupName, "#A1");
            Assert.AreEqual(10, req.ContentLength, "#A2");
            Assert.AreEqual("image/png", req.ContentType, "#A3");
            Assert.IsNull(req.Credentials, "#A4");
            Assert.AreEqual("PUT", req.Method, "#A5");
            Assert.IsFalse(req.PreAuthenticate, "#A6");
            Assert.AreEqual("file://test.txt/", req.RequestUri.AbsoluteUri, "#A7");
            Assert.AreEqual(20, req.Timeout, "#A8");

            WebHeaderCollection headers = req.Headers;

            Assert.IsNotNull(headers, "#C1");
            Assert.AreEqual(2, headers.Count, "#C2");
            Assert.AreEqual("Content-Type", req.Headers.GetKey(0), "#C3");
            Assert.AreEqual("image/png", req.Headers.Get(0), "#C4");
            Assert.AreEqual("Disposition", req.Headers.GetKey(1), "#C5");
            Assert.AreEqual("attach", req.Headers.Get(1), "#C6");

            WebProxy proxy = req.Proxy as WebProxy;

            Assert.IsNotNull(proxy, "#D1");
            Assert.AreEqual("http://proxy.ximian.com/", proxy.Address.AbsoluteUri, "#D2");
            Assert.IsNotNull(proxy.BypassArrayList, "#D3");
            Assert.AreEqual(0, proxy.BypassArrayList.Count, "#D4");
            Assert.IsNotNull(proxy.BypassList, "#D5");
            Assert.AreEqual(0, proxy.BypassList.Length, "#D6");
            Assert.IsFalse(proxy.BypassProxyOnLocal, "#D7");
            Assert.IsNull(proxy.Credentials, "#D8");
        }
        /// <summary>
        /// 开始下载
        /// </summary>
        public void StartDownload()
        {
            IsDownloading = true;
            HttpWebRequest request = (HttpWebRequest)FileWebRequest.Create(this.DownloadUrl);

            if (DownloadBytes > 0)
            {
                request.AddRange(DownloadBytes);
            }
            request.BeginGetResponse(ar =>
            {
                var response = request.EndGetResponse(ar);
                if (this.TotalBytes == 0)
                {
                    this.TotalBytes = response.ContentLength;
                }
                using (var writer = new FileStream(this.Filename, FileMode.OpenOrCreate))
                {
                    using (var stream = response.GetResponseStream())
                    {
                        while (IsDownloading)
                        {
                            byte[] data    = new byte[readBytes];
                            int readNumber = stream.Read(data, 0, data.Length);
                            if (readNumber > 0)
                            {
                                writer.Write(data, 0, readNumber);
                                this.DownloadBytes += readNumber;
                            }
                            if (this.DownloadBytes == this.TotalBytes)
                            {
                                Complete();
                            }
                        }
                    }
                }
            }, null);
        }
Exemple #5
0
        public void Timeout_Negative()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Timeout = -1;
            Assert.AreEqual(-1, req.Timeout, "#1");
            try {
                req.Timeout = -2;
                Assert.Fail("#2");
            } catch (ArgumentOutOfRangeException ex) {
                Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#3");
                Assert.IsNotNull(ex.Message, "#4");
#if !TARGET_JVM
                Assert.IsNotNull(ex.ParamName, "#5");
#if NET_2_0
                Assert.IsFalse(ex.ParamName == "value", "#6");
#else
                Assert.AreEqual("value", ex.ParamName, "#6");
#endif
#endif
                Assert.IsNull(ex.InnerException, "#7");
            }
        }
        public void ContentLength()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method        = "PUT";
            req.ContentLength = 100;
            using (Stream s = req.GetRequestStream()) {
                s.WriteByte(72);
                s.WriteByte(110);
                s.WriteByte(80);
                s.Flush();
            }
            req = (FileWebRequest)WebRequest.Create(_tempFileUri);
            using (FileWebResponse resp = (FileWebResponse)req.GetResponse()) {
                Assert.AreEqual(3, resp.ContentLength, "#1");
                Assert.AreEqual(2, resp.Headers.Count, "#2");
                Assert.AreEqual("Content-Length", resp.Headers.Keys [0], "#3");
                Assert.AreEqual("3", resp.Headers.Get(0), "#4");
                resp.Headers.Clear();
                Assert.AreEqual(3, resp.ContentLength, "#5");
                Assert.AreEqual(0, resp.Headers.Count, "#6");
            }
        }
        public void ContentType()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method      = "PUT";
            req.ContentType = "image/png";
            using (Stream s = req.GetRequestStream()) {
                s.WriteByte(72);
                s.WriteByte(110);
                s.WriteByte(80);
                s.Flush();
            }
            req = (FileWebRequest)WebRequest.Create(_tempFileUri);
            using (FileWebResponse resp = (FileWebResponse)req.GetResponse()) {
                Assert.AreEqual("application/octet-stream", resp.ContentType, "#1");
                Assert.AreEqual(2, resp.Headers.Count, "#2");
                Assert.AreEqual("Content-Type", resp.Headers.Keys [1], "#3");
                Assert.AreEqual("application/octet-stream", resp.Headers.Get(1), "#4");
                resp.Headers.Clear();
                Assert.AreEqual("application/octet-stream", resp.ContentType, "#5");
                Assert.AreEqual(0, resp.Headers.Count, "#6");
            }
        }
Exemple #8
0
        private bool makeFileRequest(string uri)
        {
            bool isRequestOk = false;

            try
            {
                Uri myUrl = new Uri(uri);
                // Create a FileWebRequest object using the passed URI.
                FileWebRequest myFileWebRequest = (FileWebRequest)WebRequest.Create(myUrl);
                // Get the FileWebResponse object.
                myFileWebResponse = (FileWebResponse)myFileWebRequest.GetResponse();
                isRequestOk       = true;
            }
            catch (WebException e)
            {
                Console.WriteLine("WebException: " + e.Message);
            }
            catch (UriFormatException e)
            {
                Console.WriteLine("UriFormatWebException: " + e.Message);
            }
            return(isRequestOk);
        }
Exemple #9
0
        private string MakeFileRequest(string Url)
        {
            Uri    uri     = new Uri(Url);
            string Content = string.Empty;

            if (uri.Scheme == Uri.UriSchemeFile)
            {
                FileWebRequest req = (FileWebRequest)FileWebRequest.Create(uri);

                FileWebResponse resp = req.GetResponse() as FileWebResponse;

                Stream stream = resp.GetResponseStream();

                using (StreamReader reader = new StreamReader(stream))
                {
                    Content = reader.ReadToEnd();
                }

                resp.Close();
            }

            return(Content);
        }
Exemple #10
0
// <Snippet1>
// <Snippet2>
    public static void GetPage(String url)
    {
        try
        {
            Uri fileUrl = new Uri("file://" + url);
            // Create a 'FileWebrequest' object with the specified Uri.
            FileWebRequest myFileWebRequest = (FileWebRequest)WebRequest.Create(fileUrl);
            // Send the 'fileWebRequest' and wait for response.
            FileWebResponse myFileWebResponse = (FileWebResponse)myFileWebRequest.GetResponse();
            // Print the ContentLength and ContentType properties received as headers in the response object.
            Console.WriteLine("\nContent length :{0}, Content Type : {1}", myFileWebResponse.ContentLength, myFileWebResponse.ContentType);
            // Release resources of response object.
            myFileWebResponse.Close();
        }
        catch (WebException e)
        {
            Console.WriteLine("\r\nWebException thrown. The Reason for failure is : {0}", e.Status);
        }
        catch (Exception e)
        {
            Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
        }
    }
Exemple #11
0
// <Snippet1>
    public static void GetPage(String url)
    {
        try
        {
            Uri fileUrl = new Uri("file://" + url);
            // Create a FileWebrequest with the specified Uri.
            FileWebRequest myFileWebRequest = (FileWebRequest)WebRequest.Create(fileUrl);
            // Send the 'fileWebRequest' and wait for response.
            FileWebResponse myFileWebResponse = (FileWebResponse)myFileWebRequest.GetResponse();
            // Process the response here.
            Console.WriteLine("\nResponse Received.Trying to Close the response stream..");
            // Release resources of response object.
            myFileWebResponse.Close();
            Console.WriteLine("\nResponse Stream successfully closed.");
        }
        catch (WebException e)
        {
            Console.WriteLine("\r\nWebException thrown. The Reason for failure is : {0}", e.Status);
        }
        catch (Exception e)
        {
            Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
        }
    }
Exemple #12
0
        private byte[] DownloadFile(string url)
        {
            try {
                var request = FileWebRequest.Create(url);
                using (var response = request.GetResponse()) {
                    using (var stream = response.GetResponseStream()) {
                        using (var memory = new MemoryStream()) {
                            stream.CopyTo(memory);
                            return(memory.ToArray());
                        }
                    }
                }
            } catch {
                _downloadErrorCount++;
                if (_downloadErrorCount <= 5)
                {
                    Program.Menu.Invoke((MethodInvoker) delegate {
                        Program.Menu.lblCurrent.Text = "Unable to download the core file " + url + " attempt " + _downloadErrorCount + "/5";
                    });
                    Application.DoEvents();
                    return(DownloadFile(url));
                }
                Program.Menu.Invoke((MethodInvoker) delegate {
                    Program.Menu.lblTotal.Visible   = false;
                    Program.Menu.prgCurrent.Visible = false;
                    Program.Menu.prgTotal.Visible   = false;
                    Program.Menu.lblCurrent.Text    = "Error: Please relaunch the updater.";
                });
                while (Program.Menu.Visible)
                {
                    Application.DoEvents();
                }
            }

            return(null);
        }
        public void ContentLength_Negative()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            try {
                req.ContentLength = -1;
                Assert.Fail("#1");
            } catch (ArgumentException ex) {
                Assert.AreEqual(typeof(ArgumentException), ex.GetType(), "#2");
                Assert.IsNotNull(ex.Message, "#3");
#if NET_2_0
                Assert.IsFalse(ex.Message == "value", "#4");
#else
                Assert.AreEqual("value", ex.Message, "#4");
#endif
#if NET_2_0
                Assert.IsNotNull(ex.ParamName, "#5");
                Assert.AreEqual("value", ex.ParamName, "#6");
#else
                Assert.IsNull(ex.ParamName, "#5");
#endif
                Assert.IsNull(ex.InnerException, "#7");
            }
        }
Exemple #14
0
        private void DownloadFile(DeliveryFile file)
        {
            WebRequest request = FileWebRequest.Create(file.SourceUrl);

            /* FTP */
            if (request.GetType().Equals(typeof(FtpWebRequest)))
            {
                FtpWebRequest ftpRequest = (FtpWebRequest)request;
                ftpRequest.UseBinary   = true;
                ftpRequest.Credentials = new NetworkCredential
                                         (
                    this.Delivery.Parameters["UserID"].ToString(),
                    this.Delivery.Parameters["Password"].ToString()
                                         );
                ftpRequest.Method     = WebRequestMethods.Ftp.DownloadFile;
                ftpRequest.UsePassive = true;
                _batch.Add(file.Download(request, Convert.ToInt64(file.Parameters["Size"])));
            }
            /*OTHER*/
            else
            {
                _batch.Add(file.Download(request));
            }
        }
Exemple #15
0
        public void GetObjectData()
        {
            FileWebRequest fwr = (FileWebRequest)WebRequest.Create("file:///test.txt");

            fwr.ConnectionGroupName = "CGN";
            fwr.ContentLength       = 10;
            fwr.ContentType         = "image/png";
            fwr.Credentials         = new NetworkCredential("Miguel", "de Icaza", "Novell");
            fwr.Headers.Add("Disposition", "attach");
            fwr.Method          = "PUT";
            fwr.PreAuthenticate = true;
            fwr.Proxy           = new WebProxy("proxy.ximian.com");
            fwr.Timeout         = 20;

            SerializationInfo si = new SerializationInfo(typeof(FileWebRequest),
                                                         new FormatterConverter());

            ((ISerializable)fwr).GetObjectData(si, new StreamingContext());
            Assert.AreEqual(9, si.MemberCount, "#A1");
            int i = 0;

            foreach (SerializationEntry entry in si)
            {
                Assert.IsNotNull(entry.Name, "#B1:" + i);
                Assert.IsNotNull(entry.ObjectType, "#B2:" + i);
                Assert.IsNotNull(entry.Value, "#B3:" + i);

                switch (i)
                {
                case 0:
                    Assert.AreEqual("headers", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(WebHeaderCollection), entry.ObjectType, "#B5:" + i);
                    break;

                case 1:
                    Assert.AreEqual("proxy", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(IWebProxy), entry.ObjectType, "#B5:" + i);
                    break;

                case 2:
                    Assert.AreEqual("uri", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(Uri), entry.ObjectType, "#B5:" + i);
                    break;

                case 3:
                    Assert.AreEqual("connectionGroupName", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(string), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual("CGN", entry.Value, "#B6:" + i);
                    break;

                case 4:
                    Assert.AreEqual("method", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(string), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual("PUT", entry.Value, "#B6:" + i);
                    break;

                case 5:
                    Assert.AreEqual("contentLength", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(long), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual(10, entry.Value, "#B6:" + i);
                    break;

                case 6:
                    Assert.AreEqual("timeout", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(int), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual(20, entry.Value, "#B6:" + i);
                    break;

                case 7:
                    Assert.AreEqual("fileAccess", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(FileAccess), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual(FileAccess.Read, entry.Value, "#B6:" + i);
                    break;

                case 8:
                    Assert.AreEqual("preauthenticate", entry.Name, "#B4:" + i);
                    Assert.AreEqual(typeof(bool), entry.ObjectType, "#B5:" + i);
                    Assert.AreEqual(false, entry.Value, "#B6:" + i);
                    break;
                }
                i++;
            }
        }
Exemple #16
0
        public void RequestUri()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            Assert.AreSame(_tempFileUri, req.RequestUri);
        }
Exemple #17
0
        public void Async()
        {
            WebResponse res = null;

            try {
                FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                req.Method        = "PUT";
                req.ContentLength = 1;
                req.ContentType   = "image/png";

                req.Timeout = 2 * 1000;
                IAsyncResult async = req.BeginGetRequestStream(null, null);
                try {
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#1 should've failed");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                    // a previous call is still in progress
                }

                try {
                    req.GetRequestStream();
                    Assert.Fail("#3 should've failed");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse while
                    // a previous call is still in progress
                }

                using (Stream wstream = req.EndGetRequestStream(async)) {
                    Assert.IsFalse(wstream.CanRead, "#1r");
                    Assert.IsTrue(wstream.CanWrite, "#1w");
                    Assert.IsTrue(wstream.CanSeek, "#1s");

                    wstream.WriteByte(72);
                    wstream.WriteByte(101);
                    wstream.WriteByte(108);
                    wstream.WriteByte(108);
                    wstream.WriteByte(111);
                    wstream.Close();
                }

                Assert.AreEqual(1, req.ContentLength, "#1cl");
                Assert.AreEqual("image/png", req.ContentType, "#1ct");

                // stream written

                req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                res = req.GetResponse();

                try {
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#20: should've failed");
                } catch (InvalidOperationException) {
                    // Cannot send a content-body with this verb-type
                }

                try {
                    req.Method = "PUT";
                    req.BeginGetRequestStream(null, null);
                    Assert.Fail("#21: should've failed");
                } catch (InvalidOperationException) {
                    // This operation cannot be perfomed after the request has been submitted.
                }

                req.GetResponse();

                IAsyncResult async2 = req.BeginGetResponse(null, null);

                // this succeeds !!
                WebResponse res2 = req.EndGetResponse(async2);
                Assert.AreSame(res, res2, "#23");

                Assert.AreEqual(5, res.ContentLength, "#2 len");
                Assert.AreEqual("application/octet-stream", res.ContentType, "#2 type");
                Assert.AreEqual("file", res.ResponseUri.Scheme, "#2 scheme");

                Stream rstream = res.GetResponseStream();
                Assert.IsTrue(rstream.CanRead, "#3r");
                Assert.IsFalse(rstream.CanWrite, "#3w");
                Assert.IsTrue(rstream.CanSeek, "#3s");

                Assert.AreEqual(72, rstream.ReadByte(), "#4a");
                Assert.AreEqual(101, rstream.ReadByte(), "#4b");
                Assert.AreEqual(108, rstream.ReadByte(), "#4c");
                Assert.AreEqual(108, rstream.ReadByte(), "#4d");
                Assert.AreEqual(111, rstream.ReadByte(), "#4e");

                rstream.Close();

                try {
                    long len = res.ContentLength;
                    Assert.AreEqual((long)5, len, "#5");
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed contentlength");
                }
                try {
                    WebHeaderCollection w = res.Headers;
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed headers");
                }
                try {
                    res.Close();
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed close");
                }
            } finally {
                if (res != null)
                {
                    res.Close();
                }
            }
        }
Exemple #18
0
        public void GetRequestStream()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(
                _tempFileUri);

            req.Timeout = 1000;
            req.Method  = "POST";
            FileStream fsA = null;
            FileStream fsB = null;

            try {
                fsA = req.GetRequestStream() as FileStream;
                Assert.IsNotNull(fsA, "#A1");
                try {
                    req.GetRequestStream();
                    Assert.Fail("#A2");
                } catch (WebException) {
                    // The operation has timed out
                }
                fsA.Close();
                try {
                    req.GetRequestStream();
                    Assert.Fail("#A3");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse
                    // while a previous call is still in progress.
                }
            } finally {
                if (fsA != null)
                {
                    fsA.Close();
                }
                if (fsB != null)
                {
                    fsB.Close();
                }
            }

            req         = (FileWebRequest)WebRequest.Create(_tempFileUri);
            req.Timeout = 1000;
            req.Method  = "POST";
            try {
                fsA = req.GetRequestStream() as FileStream;
                Assert.IsNotNull(fsA, "#B1");
                fsA.Close();
                try {
                    req.GetRequestStream();
                    Assert.Fail("#B2");
                } catch (WebException) {
                    // The operation has timed out
                }
                fsA.Close();
                try {
                    req.GetRequestStream();
                    Assert.Fail("#B3");
                } catch (InvalidOperationException) {
                    // Cannot re-call BeginGetRequestStream/BeginGetResponse
                    // while a previous call is still in progress.
                }
            } finally {
                if (fsA != null)
                {
                    fsA.Close();
                }
                if (fsB != null)
                {
                    fsB.Close();
                }
            }
        }
Exemple #19
0
        public void Sync()
        {
            WebResponse res = null;

            try {
                FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                req.ContentLength = 1;
                req.ContentType   = "image/png";

                try {
                    Stream stream = req.GetRequestStream();
                    Assert.Fail("should throw exception");
                } catch (ProtocolViolationException) {
                }

                req.Method = "PUT";

                Stream wstream = req.GetRequestStream();
                Assert.IsFalse(wstream.CanRead, "#1r");
                Assert.IsTrue(wstream.CanWrite, "#1w");
                Assert.IsTrue(wstream.CanSeek, "#1s");

                wstream.WriteByte(72);
                wstream.WriteByte(101);
                wstream.WriteByte(108);
                wstream.WriteByte(108);
                wstream.WriteByte(111);
                wstream.Close();

                Assert.AreEqual(1, req.ContentLength, "#1cl");
                Assert.AreEqual("image/png", req.ContentType, "#1ct");

                // stream written

                req = (FileWebRequest)WebRequest.Create(_tempFileUri);
                res = req.GetResponse();
                Assert.AreEqual((long)5, res.ContentLength, "#2 len");
                Assert.AreEqual("application/octet-stream", res.ContentType, "#2 type");
                Assert.AreEqual("file", res.ResponseUri.Scheme, "#2 scheme");

                Stream rstream = res.GetResponseStream();
                Assert.IsTrue(rstream.CanRead, "#3r");
                Assert.IsFalse(rstream.CanWrite, "#3w");
                Assert.IsTrue(rstream.CanSeek, "#3s");

                Assert.AreEqual(72, rstream.ReadByte(), "#4a");
                Assert.AreEqual(101, rstream.ReadByte(), "#4b");
                Assert.AreEqual(108, rstream.ReadByte(), "#4c");
                Assert.AreEqual(108, rstream.ReadByte(), "#4d");
                Assert.AreEqual(111, rstream.ReadByte(), "#4e");

                rstream.Close();

                try {
                    long len = res.ContentLength;
                    Assert.AreEqual((long)5, len, "#5");
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed contentlength");
                }
                try {
                    WebHeaderCollection w = res.Headers;
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed headers");
                }
                try {
                    res.Close();
                } catch (ObjectDisposedException) {
                    Assert.Fail("#disposed close");
                }
            } finally {
                if (res != null)
                {
                    res.Close();
                }
            }
        }
        public void GetResponseStream()
        {
            FileWebRequest req = (FileWebRequest)WebRequest.Create(_tempFileUri);

            req.Method      = "PUT";
            req.ContentType = "image/png";
            using (Stream s = req.GetRequestStream()) {
                s.WriteByte(72);
                s.WriteByte(110);
                s.WriteByte(80);
                s.Flush();
            }
            req = (FileWebRequest)WebRequest.Create(_tempFileUri);
            FileWebResponse respA = null;
            FileWebResponse respB = null;
            FileStream      fsA   = null;
            FileStream      fsB   = null;

            try {
                respA = (FileWebResponse)req.GetResponse();
                fsA   = respA.GetResponseStream() as FileStream;
                Assert.IsNotNull(fsA, "#A1");
                Assert.IsTrue(fsA.CanRead, "#A2");
                Assert.IsTrue(fsA.CanSeek, "#A3");
                Assert.IsFalse(fsA.CanTimeout, "#A4");
                Assert.IsFalse(fsA.CanWrite, "#A5");
                Assert.AreEqual(3, fsA.Length, "#A6");
                Assert.AreEqual(0, fsA.Position, "#A7");
                try {
                    int i = fsA.ReadTimeout;
                    Assert.Fail("#A8:" + i);
                } catch (InvalidOperationException) {
                }
                try {
                    int i = fsA.WriteTimeout;
                    Assert.Fail("#A9:" + i);
                } catch (InvalidOperationException) {
                }

                respB = (FileWebResponse)req.GetResponse();
                fsB   = respB.GetResponseStream() as FileStream;
                Assert.IsNotNull(fsB, "#B1");
                Assert.IsTrue(fsB.CanRead, "#B2");
                Assert.IsTrue(fsB.CanSeek, "#B3");
                Assert.IsFalse(fsB.CanTimeout, "#B4");
                Assert.IsFalse(fsB.CanWrite, "#B5");
                Assert.AreEqual(3, fsB.Length, "#B6");
                Assert.AreEqual(0, fsB.Position, "#B7");
                try {
                    int i = fsB.ReadTimeout;
                    Assert.Fail("#B8:" + i);
                } catch (InvalidOperationException) {
                }
                try {
                    int i = fsB.WriteTimeout;
                    Assert.Fail("#B9:" + i);
                } catch (InvalidOperationException) {
                }
            } finally {
                if (respA != null)
                {
                    respA.Close();
                }
                if (respB != null)
                {
                    respB.Close();
                }
            }
        }
        //這裡的Start請開執行緒去跑
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowLawItem = list[0];
                            list.Remove(NowLawItem);
                        }
                    }
                    if (NowLawItem != null)
                    {
                        try
                        {
                            DownloadFileStart(NowLawItem); //開始

                            HttpWebRequest request = (HttpWebRequest)FileWebRequest.Create(NowLawItem.Link);

                            //Wayne 20150429
                            request.CachePolicy            = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Revalidate);
                            request.Proxy                  = null;
                            request.AutomaticDecompression = DecompressionMethods.GZip;

                            if (NowLawItem.DownloadBytes > 0)
                            {
                                //if (File.Exists(NowLawItem.StorageFilePath) == true)
                                //{
                                //    NowLawItem.DownloadBytes = new FileInfo(NowLawItem.StorageFilePath).Length;
                                //    if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                //    {
                                //        downloaderType = DownloaderType.檔案下載完成;
                                //        // 更新資料
                                //        //UpdateToDB(NowFileItem);
                                //        goto TypeControl;
                                //    }
                                //    else
                                //    {
                                //        request.AddRange(NowLawItem.DownloadBytes);
                                //        //UpdateToDB(NowFileItem);
                                //    }
                                //}
                                //else
                                //{
                                //request.AddRange(NowLawItem.DownloadBytes);
                                // 更新資料
                                //UpdateToDB(NowFileItem);
                                //}
                            }

                            //取得回應
                            WebResponse response = request.GetResponse();

                            if (NowLawItem.TotalBytes == 0)
                            {
                                NowLawItem.TotalBytes = response.ContentLength;
                            }

                            //先建立檔案要存放的資料夾
                            Directory.CreateDirectory(NowLawItem.StorageFileFolder);
                            using (var writer = new FileStream(NowLawItem.StorageFilePath, FileMode.OpenOrCreate))
                            {
                                try
                                {
                                    writer.Seek(0, SeekOrigin.End);
                                    using (var stream = response.GetResponseStream())
                                    {
                                        while (NowLawItem.DownloadBytes < NowLawItem.TotalBytes) //暫停條件設定成 fileItem的屬性 IsPause =true 就停止
                                        {
                                            // 暫停和停止動作一定要寫在while裡
                                            if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                            {
                                                //UpdateToDB(NowLawItem);
                                                break;
                                            }

                                            byte[] data       = new byte[buffer];
                                            int    readNumber = stream.Read(data, 0, data.Length);
                                            if (readNumber > 0)
                                            {
                                                writer.Write(data, 0, readNumber);
                                                writer.Flush();
                                                NowLawItem.DownloadBytes += readNumber;
                                            }

                                            if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                            {
                                                //先寫入檔案,並且關閉檔案使用權。
                                                downloaderType = DownloaderType.檔案下載完成;
                                                break;
                                            }
                                            else
                                            {
                                                if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                                {
                                                    //UpdateToDB(NowLawItem);
                                                    break;
                                                }
                                                else
                                                {
                                                    double percentage = GetPercent(NowLawItem.DownloadBytes, NowLawItem.TotalBytes);
                                                    if (percentage - NowLawItem.LastPercentage > PaperLess_Emeeting.Properties.Settings.Default.Downloader_InvokePercent)
                                                    {
                                                        NowLawItem.LastPercentage = percentage;
                                                        // 進度條百分比callback
                                                        DownloadProgressChanged(NowLawItem);
                                                        //UpdateToDB(NowLawItem);
                                                        //Thread.Sleep(1);
                                                    }
                                                }
                                            }
                                        }

                                        if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                        {
                                            DownloadProgressChanged(NowLawItem);
                                            //先寫入檔案,並且關閉檔案使用權。
                                            downloaderType = DownloaderType.檔案下載完成;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    downloaderType = DownloaderType.出錯;
                                    try
                                    {
                                        if (Home_UnZipError_Event != null)
                                        {
                                            Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                                                , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                                                , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                            Thread.Sleep(1100);
                                        }
                                        LogTool.Debug(ex);
                                    }
                                    catch
                                    {
                                        downloaderType = DownloaderType.出錯;
                                    }
                                }
                            }
                        }
                        catch (Exception ex2)
                        {
                            downloaderType = DownloaderType.出錯;
                            try
                            {
                                if (Home_UnZipError_Event != null)
                                {
                                    Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                                        , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                                        , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                    Thread.Sleep(1100);
                                }
                                LogTool.Debug(ex2);
                            }
                            catch
                            {
                                downloaderType = DownloaderType.出錯;
                            }
                        }
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
            }
            catch (Exception ex)
            {
                downloaderType = DownloaderType.出錯;
                try
                {
                    if (Home_UnZipError_Event != null)
                    {
                        Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                            , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                            , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                        Thread.Sleep(1100);
                    }
                    LogTool.Debug(ex);
                }
                catch
                {
                    downloaderType = DownloaderType.出錯;
                }
            }

TypeControl:
            // 在這裡統整所有的控制行為 錯誤,暫停,停止,完成
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
                //StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.停止:
                //NowLawItem = null;
                //lock (thisLock)
                //{
                //    list.Clear();
                //}
                //downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowLawItem);
                // 不要開新的下載
                break;

            case DownloaderType.出錯:
                // 呼叫 Error callback
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.暫停:
                // 呼叫 Pause callback
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.檔案下載完成:
                // 呼叫 FileCompleted callback
                // 下面會等待到解壓縮完,才會繼續下一個檔案的下載
                DownloadFileCompleted(NowLawItem);
                StartNextFileItemDownload(NowLawItem);
                break;
            }
        }
Exemple #22
0
        /// <summary>
        /// Can be called synchronized to get a HttpWebResponse.
        /// </summary>
        /// <param name="method">The HTTP method being used</param>
        /// <param name="address">Url to request</param>
        /// <param name="credentials">Url credentials</param>
        /// <param name="userAgent"></param>
        /// <param name="proxy">Proxy to use</param>
        /// <param name="ifModifiedSince">Header date</param>
        /// <param name="eTag">Header tag</param>
        /// <param name="timeout">Request timeout. E.g. 60 * 1000, means one minute timeout.
        /// If zero or less than zero, the default timeout of one minute will be used</param>
        /// <param name="cookie">HTTP cookie to send along with the request</param>
        /// <param name="body">The body of the request (if it is POST request)</param>
        /// <param name="additonalHeaders">These are additional headers that are being specified to the Web request</param>
        /// <returns>WebResponse</returns>
        public static WebResponse GetResponse(HttpMethod method, string address, ICredentials credentials,
                                              string userAgent,
                                              IWebProxy proxy, DateTime ifModifiedSince, string eTag, int timeout,
                                              Cookie cookie, string body, WebHeaderCollection additonalHeaders)
        {
            try
            {
                WebRequest webRequest = WebRequest.Create(address);

                HttpWebRequest httpRequest = webRequest as HttpWebRequest;
                FileWebRequest fileRequest = webRequest as FileWebRequest;

                if (httpRequest != null)
                {
                    httpRequest.Timeout = (timeout <= 0 ? DefaultTimeout : timeout);
                    //two minute timeout, if lower than zero
                    httpRequest.UserAgent         = userAgent ?? FullUserAgent(userAgent);
                    httpRequest.Proxy             = proxy;
                    httpRequest.AllowAutoRedirect = false;
                    httpRequest.IfModifiedSince   = ifModifiedSince;
                    //httpRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
                    httpRequest.AutomaticDecompression = DecompressionMethods.GZip |
                                                         DecompressionMethods.Deflate;
                    httpRequest.Method = method.ToString().ToUpperInvariant();

                    if (additonalHeaders != null)
                    {
                        httpRequest.Headers.Add(additonalHeaders);
                    }

                    if (cookie != null)
                    {
                        httpRequest.CookieContainer = new CookieContainer();
                        httpRequest.CookieContainer.Add(cookie);
                    }

                    if (eTag != null)
                    {
                        httpRequest.Headers.Add("If-None-Match", eTag);
                        httpRequest.Headers.Add("A-IM", "feed");
                    }

                    if (credentials != null)
                    {
                        httpRequest.Credentials = credentials;
                    }

                    if (method != HttpMethod.Get && !StringHelper.EmptyTrimOrNull(body))
                    {
                        UTF8Encoding encoding = new UTF8Encoding();
                        byte[]       data     = encoding.GetBytes(body);
                        httpRequest.ContentType = (body.StartsWith("<")
                                                       ? "application/xml"
                                                       : "application/x-www-form-urlencoded");
                        httpRequest.ContentLength = data.Length;
                        Stream newStream = httpRequest.GetRequestStream();
                        newStream.Write(data, 0, data.Length);
                        newStream.Close();
                    }
                }
                else if (fileRequest != null)
                {
                    fileRequest.Timeout = (timeout <= 0 ? DefaultTimeout : timeout);
                    if (credentials != null)
                    {
                        fileRequest.Credentials = credentials;
                    }
                }
                else
                {
                    Debug.Assert(false,
                                 "GetSyncResponse(): unhandled WebRequest type: " + webRequest.GetType());
                }

                return(webRequest.GetResponse());
            }
            catch (Exception e)
            {
                //For some reason the HttpWebResponse class throws an exception on 3xx responses

                WebException we = e as WebException;

                if ((we != null) && (we.Response != null))
                {
                    return(we.Response);
                }
                throw;
            } //end try/catch
        }
Exemple #23
0
        /// <summary>
        /// Called to extract a list from the specified URL.
        /// </summary>
        /// <param name=”url”>The URL to extract the list
        /// from.</param>
        /// <param name=”listType”>What type of list, specify
        /// its beginning tag (i.e. <UL>)</param>
        /// <param name=”optionList”>Which list to search,
        /// zero for first.</param>

        public void ProcessTable(Uri url, int tableNum)
        {

            //ignore bad cert code
            IgnoreBadCertificates();

            //code to allow program with work with different authentication schemes
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            List<String> list = new List<String>();
            //WebRequest http = HttpWebRequest.Create(url);
            FileWebRequest http = (FileWebRequest)WebRequest.Create(url);
            //HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            FileWebResponse response = (FileWebResponse)http.GetResponse();
            Stream istream = response.GetResponseStream();
            ParseHTML parse = new ParseHTML(istream);
            StringBuilder buffer = new StringBuilder();
            bool capture = false;
            Advance(parse, "table", tableNum);
            int ch;
            while ((ch = parse.Read()) != -1)
            {
                if (ch == 0)
                {
                    HTMLTag tag = parse.Tag;
                    if (String.Compare(tag.Name, "tr", true) == 0)
                    {
                        list.Clear();
                        capture = false;
                        buffer.Length = 0;
                    }
                    else if (String.Compare(tag.Name, "/tr", true) == 0)
                    {
                        if (list.Count > 0)
                        {
                            ProcessTableRow(list);
                            list.Clear();
                        }
                    }
                    else if (String.Compare(tag.Name, "td", true) == 0)
                    {
                        if (buffer.Length > 0)
                            list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = true;
                    }
                    else if (String.Compare(tag.Name, "/td", true) == 0)
                    {
                        list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = false;
                    }
                    else if (String.Compare(tag.Name, "/table", true) == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (capture)
                        buffer.Append((char)ch);
                }
            }
        }//end ProcessTable
Exemple #24
0
        /// <summary>
        /// returns a collection of bytes from a Url
        /// </summary>
        /// <param name="url">The URL to retrieve</param>
        public void GetUrlData(string url)
        {
            Uri uri = new Uri(url);

            if (!uri.IsFile)
            {
                throw new UriFormatException("url is not a local file");
            }

            FileWebRequest request = WebRequest.Create(url) as FileWebRequest;

            if (request == null)
            {
                this.Clear();
                return;
            }

            request.Method = "GET";

            // download the target URL
            FileWebResponse response = (FileWebResponse)request.GetResponse();

            // convert response stream to byte array
            using (Stream stream = response.GetResponseStream())
            {
                ExtendedBinaryReader extReader = new ExtendedBinaryReader(stream);
                _ResponseBytes = extReader.ReadToEnd();
            }

            // For local operations, we consider the data are never compressed. Else, the "Content-Encoding" field
            // in the headers would be "gzip" or "deflate". This could be handled quite easily with SharpZipLib for instance.

            // sometimes URL is indeterminate, eg, "http://website.com/myfolder"
            // in that case the folder and file resolution MUST be done on
            // the server, and returned to the client as ContentLocation
            _ContentLocation = response.Headers["Content-Location"];
            if (_ContentLocation == null)
            {
                _ContentLocation = "";
            }

            // if we have string content, determine encoding type
            // (must cast to prevent null)
            // HACK We determine the content type based on the uri extension,
            // as the header returned by the FileWebResponse is always "application/octet-stream" (hard coded in .NET!!)
            // text/html
            string ext = Path.GetExtension(uri.LocalPath).TrimStart(new char[] { '.' });

            switch (ext)
            {
            // What's important here is to identify TEXT mime types. Because, the default will resort to binary file.
            case "htm":
            case "html":    _DetectedContentType = "text/html";                     break;

            case "css":             _DetectedContentType = "text/css";                      break;

            case "csv":             _DetectedContentType = "text/csv";                      break;

            case "rtf":             _DetectedContentType = "text/rtf";                      break;

            case "aspx":
            case "xsl":
            case "xml":             _DetectedContentType = "text/xml";                      break;

            case "bmp":             _DetectedContentType = "image/bmp";                     break;

            case "gif":             _DetectedContentType = "image/gif";                     break;

            case "ico":             _DetectedContentType = "image/x-icon";          break;

            case "jpg":
            case "jpeg":    _DetectedContentType = "image/jpeg";            break;

            case "png":             _DetectedContentType = "image/png";                     break;

            case "tif":
            case "tiff":    _DetectedContentType = "image/tiff";            break;

            case "js":              _DetectedContentType = "application/x-javascript";                      break;

            default:
                // Line commented: we don't change it
                _DetectedContentType = response.Headers["Content-Type"];                                // Always "application/octet-stream" ...
                break;
            }
            if (_DetectedContentType == null)
            {
                _DetectedContentType = "";
            }
            if (ResponseIsBinary)
            {
                _DetectedEncoding = null;
            }
            else if (_ForcedEncoding == null)
            {
                _DetectedEncoding = DetectEncoding(_DetectedContentType, _ResponseBytes);
            }
        }
Exemple #25
0
        private void DownloadFile(string FileName, string strUrl)
        {
            HttpWebRequest  webRequest;
            HttpWebResponse webResponse = null;
            FileWebRequest  fileRequest;
            FileWebResponse fileResponse = null;
            bool            isFile       = false;

            try
            {
                System.Globalization.DateTimeFormatInfo dfi = null;
                System.Globalization.CultureInfo        ci  = null;
                ci  = new System.Globalization.CultureInfo("zh-CN");
                dfi = new System.Globalization.DateTimeFormatInfo();

                //WebRequest wr = WebRequest.Create("");

                //System.Net.WebResponse w=wr.
                DateTime      fileDate;
                long          totalBytes;
                DirectoryInfo theFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
                string        fileName  = Path.Combine(theFolder.FullName, FileName);

                isFile = (HttpWebRequest.Create(strUrl) is FileWebRequest);

                if (isFile)
                {
                    fileRequest  = (FileWebRequest)FileWebRequest.Create(strUrl);
                    fileResponse = (FileWebResponse)fileRequest.GetResponse();
                    if (fileResponse == null)
                    {
                        return;
                    }
                    fileDate   = DateTime.Now;
                    totalBytes = fileResponse.ContentLength;
                }
                else
                {
                    webRequest  = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    if (webResponse == null)
                    {
                        return;
                    }
                    fileDate   = webResponse.LastModified;
                    totalBytes = webResponse.ContentLength;
                }

                //pbUpdate.Maximum = Convert.ToInt32(totalBytes);

                Stream stream;
                if (isFile)
                {
                    stream = fileResponse.GetResponseStream();
                }
                else
                {
                    stream = webResponse.GetResponseStream();
                }
                FileStream sw = new FileStream(fileName, FileMode.Create);
                int        totalDownloadedByte = 0;
                Byte[]     @by   = new byte[1024];
                int        osize = stream.Read(@by, 0, @by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    sw.Write(@by, 0, osize);
                    //pbUpdate.Value = totalDownloadedByte;
                    osize = stream.Read(@by, 0, @by.Length);
                }
                sw.Close();
                stream.Close();

                File.SetLastWriteTime(FileName, fileDate);
            }
            catch //(Exception ex)
            {
                if (fileResponse != null)
                {
                    fileResponse.Close();
                }

                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
        }
Exemple #26
0
        /// <summary>
        /// Process the request (once). Get the response (page content, cookies, ...).
        /// </summary>
        private void ProcessRequest()
        {
            if (RequestProcessed)
            {
                return; // test, unlocked
            }
            lock (this)
            {
                if (RequestProcessed)
                {
                    return;                   // double checked lock, test again
                }
                RequestProcessed = true;

                StorageValue data = (StorageValue)DataCache.GetItem(StorageKey,
                                                                    delegate(out DateTime expiration)
                {
                    WebRequest webreq = WebRequest.Create(ContextUri);

                    string RespContent           = null;
                    CookieCollection RespCookies = null;

                    if (webreq is HttpWebRequest)
                    {
                        HttpWebRequest req = (HttpWebRequest)webreq;

                        req.Referer           = (RefererContext != null && RefererContext.ContextUri != null) ? RefererContext.ContextUri.AbsoluteUri : null;
                        req.UserAgent         = DefaultUserAgent;
                        req.Timeout           = 30000;
                        req.AllowAutoRedirect = false;
                        req.KeepAlive         = false;
                        var cookieJar         = req.CookieContainer = new CookieContainer();
                        if (RefererContext != null && RefererContext.Cookies != null)
                        {       // TODO: filter cookies by domain and path
                            req.CookieContainer.Add(RefererContext.Cookies);
                        }
                        //req.Headers.Add("Accept-Language", "en,cs");

                        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // cookies
                        foreach (Cookie c in cookieJar.GetCookies(req.RequestUri))
                        {
                            if (RespCookies == null)
                            {
                                RespCookies = new CookieCollection();
                            }

                            RespCookies.Add(c);
                        }

                        // TODO: headers (language, cache expire, content type, encoding, Response URI, ...)

                        // close the response
                        resp.Close();
                    }
                    else if (webreq is FileWebRequest)
                    {
                        FileWebRequest req   = (FileWebRequest)webreq;
                        FileWebResponse resp = (FileWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // close the response
                        resp.Close();
                    }

                    expiration = DateTime.Now.AddHours(1.0);        // TODO: time based on HTML header or HtmlContext parameters

                    return(new StorageValue()
                    {
                        Content = RespContent, Cookies = RespCookies
                    });
                }
                                                                    );

                _Content = data.Content;
                _Cookies = data.Cookies;
            }
        }
Exemple #27
0
        //Uploading the generated file to FTP server
        public static void UploadFileToFtp(string BAI2)
        {
            try
            {
                var fileName = "";
                Console.WriteLine("---Uploading File---");
                //Path where test file stored
                string sourcePath = dirPath + @"\Resources\FTPFileGenerated\";
                //Invalid file to upload
                if (BAI2 != "Invalid")
                {
                    fileName = filename + ".txt";
                }
                //Valid file to upload
                else
                {
                    fileName = filename;
                }

                // Use Path class to manipulate file and directory paths.
                string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                destFile = System.IO.Path.Combine(FTPPath, fileName);

                //FileStream for holding the file
                FileStream fStream = new FileStream(destFile, FileMode.Create);

                //connect to the server
                FileWebRequest fileRequest = (FileWebRequest)FtpWebRequest.Create(new Uri(sourceFile));

                //set the protocol for the request
                fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                string username   = WebUtilities.fetchParamValFromConfig("username");
                string password   = WebUtilities.fetchParamValFromConfig("password");
                string dePassword = WebUtilities.decryptString(password);

                //provide username and password
                fileRequest.Credentials = new NetworkCredential(username, dePassword);

                //get the servers response
                WebResponse response = fileRequest.GetResponse();

                //retrieve the response stream
                Stream stream = response.GetResponseStream();

                //create byte buffer
                byte[] buffer = new byte[1024];
                long   size   = 0;

                //determine how much has been read
                int totalRead = stream.Read(buffer, 0, buffer.Length);

                //loop through the total size of the file
                while (totalRead > 0)
                {
                    size += totalRead;

                    //write to the stream
                    fStream.Write(buffer, 0, totalRead);

                    //get remaining size
                    totalRead = stream.Read(buffer, 0, 1024);
                }
                // Close the streams.
                fStream.Close();
                stream.Close();
                fileExistsInFTPflag = true;
            }
            catch (Exception e)
            {
                Assert.Fail("File is not successfully uploaded into FTP folder location: " + e);
            }
        }
        //這裡的Start請開執行序去跑
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowFileItem = list[0];
                            list.Remove(NowFileItem);
                        }
                    }
                    if (NowFileItem != null)
                    {
                        DownloadFileStart(NowFileItem); //開始

                        HttpWebRequest request = (HttpWebRequest)FileWebRequest.Create(NowFileItem.Url);
                        //Wayne 20150429
                        request.CachePolicy            = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Revalidate);
                        request.Proxy                  = null;
                        request.AutomaticDecompression = DecompressionMethods.GZip;
                        // 不要使用這個,取得或設定值,指出是否要分區段傳送資料至網際網路資源。
                        //request.SendChunked = true;
                        if (NowFileItem.DownloadBytes > 0)
                        {
                            if (File.Exists(NowFileItem.StorageFilePath) == true)
                            {
                                NowFileItem.DownloadBytes = new FileInfo(NowFileItem.StorageFilePath).Length;
                                if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                {
                                    downloaderType = DownloaderType.檔案下載完成;
                                    // 更新資料
                                    //UpdateToDB(NowFileItem);
                                    goto TypeControl;
                                }
                                else
                                {
                                    request.AddRange(NowFileItem.DownloadBytes);
                                    //UpdateToDB(NowFileItem);
                                }
                            }
                            else
                            {
                                request.AddRange(NowFileItem.DownloadBytes);
                                // 更新資料
                                //UpdateToDB(NowFileItem);
                            }
                        }

                        //取得回應
                        WebResponse response = request.GetResponse();

                        if (NowFileItem.TotalBytes == 0)
                        {
                            NowFileItem.TotalBytes = response.ContentLength;
                        }

                        //先建立檔案要存放的資料夾
                        Directory.CreateDirectory(NowFileItem.StorageFileFolder);

                        //FileMode fileMode = FileMode.Create;
                        //if (File.Exists(NowFileItem.StorageFilePath) == true)
                        //{
                        //    fileMode = FileMode.Open;
                        //}

                        using (var writer = new FileStream(NowFileItem.StorageFilePath, FileMode.OpenOrCreate))
                        {
                            try
                            {
                                writer.Seek(0, SeekOrigin.End);
                                using (var stream = response.GetResponseStream())
                                {
                                    while (NowFileItem.DownloadBytes < NowFileItem.TotalBytes)  //暫停條件設定成 fileItem的屬性 IsPause =true 就停止
                                    {
                                        // 暫停和停止動作一定要寫在while裡
                                        if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                        {
                                            // 可存可不存,各有優缺點
                                            // 存起來的話缺點為user暫停後,馬上重整可能會多1%進度
                                            // 不存的話不會有以上缺點,因為AddRange是從FileStream裡面讀取的所以不影響
                                            UpdateToDB(NowFileItem);
                                            break;
                                        }

                                        byte[] data       = new byte[buffer];
                                        int    readNumber = stream.Read(data, 0, data.Length);

                                        if (readNumber > 0)
                                        {
                                            writer.Write(data, 0, readNumber);
                                            writer.Flush();
                                            NowFileItem.DownloadBytes += readNumber;
                                        }

                                        if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                        {
                                            //先寫入檔案,並且關閉檔案使用權。
                                            downloaderType = DownloaderType.檔案下載完成;
                                            //UpdateToDB(NowFileItem);
                                            break;
                                        }
                                        else
                                        {
                                            if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                            {
                                                UpdateToDB(NowFileItem);
                                                break;
                                            }
                                            else
                                            {
                                                double percentage = GetPercent(NowFileItem.DownloadBytes, NowFileItem.TotalBytes);

                                                // 加速MeetingRoom的事件接收,才不會讓MeetingRoom的下載事件開始的觸發
                                                // 被下面的1%的限制給延後觸發MeetingRoom的開始下載。
                                                if (MeetingRoom_DownloadFileStart_Event != null)
                                                {
                                                    MeetingRoom_DownloadFileStart_Event(NowFileItem);
                                                }

                                                if (percentage - NowFileItem.LastPercentage > PaperLess_Emeeting.Properties.Settings.Default.Downloader_InvokePercent)
                                                {
                                                    NowFileItem.LastPercentage = percentage;
                                                    // 進度條百分比callback
                                                    DownloadProgressChanged(NowFileItem);
                                                    UpdateToDB(NowFileItem);
                                                    //Thread.Sleep(1);
                                                }
                                            }
                                        }
                                    }

                                    if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                    {
                                        DownloadProgressChanged(NowFileItem);
                                        //先寫入檔案,並且關閉檔案使用權。
                                        downloaderType = DownloaderType.檔案下載完成;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                downloaderType = DownloaderType.出錯;
                                DownloadError(NowFileItem);
                                LogTool.Debug(ex);
                            }
                        }
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
            }
            catch (Exception ex)
            {
                downloaderType = DownloaderType.出錯;
                DownloadError(NowFileItem);
                LogTool.Debug(ex);
            }



TypeControl:
            // 在這裡統整所有的控制行為 錯誤,暫停,停止,完成
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
                //StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.停止:
                // 呼叫 Stop callback
                // 停止可以再呼叫一次StartNextFileItemDownload(NowFileItem);
                // 避免下載器的狀態跑到停止後,可能會沒有被重置成 DownloaderType.沒有任何檔案下載中;
                //NowFileItem = null;
                //lock (thisLock)
                //{
                //    list.Clear();
                //}
                //downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowFileItem);
                // 不要開新的下載
                break;

            case DownloaderType.出錯:
                // 呼叫 Error callback
                // 儲存下載狀態
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.暫停:
                // 呼叫 Pause callback
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.檔案下載完成:
                // 呼叫 FileCompleted callback
                DownloadFileCompleted(NowFileItem);
                StartNextFileItemDownload(NowFileItem);
                break;
            }
        }
 public RequestDeclare()
 {
     myFileWebRequest = null;
 }
Exemple #30
0
        void Button1Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "HTML|*.html";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Clear();
                //INIZIALIZZAZIONE VARIABILI PER FILTRI CON CHECKBOX
                string puntoSpazio  = "";
                string spazioPunto  = "";
                string puntoSingolo = "";
                if (checkBox1.Checked)
                {
                    puntoSpazio = ". ";
                }
                if (checkBox2.Checked)
                {
                    spazioPunto = " .";
                }
                if (checkBox3.Checked)
                {
                    puntoSingolo = ".";
                }
                //--------------------------------------------------

                string filePath = ofd.FileName;                                 //OTTIENE LA STRINGA CORRISPONDENTE AL PERCORSO DEL FILE SELEZIONATO
                //----------CODICE-PER-PRENDERE-IL-SORGENTE-DELL'HTML-------------
                FileWebRequest  request    = (FileWebRequest)WebRequest.Create(filePath);
                FileWebResponse response   = (FileWebResponse)request.GetResponse();
                StreamReader    sr         = new StreamReader(response.GetResponseStream());
                String          htmlSource = sr.ReadToEnd();
                //----------------------------------------------------------------
                //Dentro htmlSource c'è una il sorgente

                //string[] testSplit = htmlSource.Split("<tr id=\"r1\">");
                string[] htmlSplitted = null;
                htmlSplitted = htmlSource.Split(new[] { "<tr id=" }, StringSplitOptions.None);



                //richTextBox1.Text = htmlSplitted2[0];

                double t = 100 / htmlSplitted.Length;

                for (int i = 1; i < htmlSplitted.Length; i++)
                {
                    string[] htmlSplitted1 = null;
                    htmlSplitted1 = htmlSplitted[i].Split(new[] { "<td class=\"td2\">" }, StringSplitOptions.None);
                    //--------------------------------MODIFICHE PER AGGIUNGERE ID E SOURCE-------------------------------------------------
                    string[] htmlSplitSup = null;                                                                                       //Variabile che creo per prendere anche id source
                    htmlSplitSup = htmlSplitted1[0].Split(new[] { "</td>" }, StringSplitOptions.None);

                    string[] htmlSplitSup2 = null;
                    htmlSplitSup2 = htmlSplitSup[0].Split(new[] { ">" }, StringSplitOptions.None);

                    string[] htmlSplitSup3 = null;
                    htmlSplitSup3 = htmlSplitSup[1].Split(new[] { "<td>" }, StringSplitOptions.None);

                    string id     = htmlSplitSup2[2];
                    string source = htmlSplitSup3[1];
                    //-----------------------------------------------------------------------------------------------------------------
                    if (htmlSplitted1.Length < 2)
                    {
                        continue;
                    }
                    //LA STRINGA DI TARGHE E' DENTRO htmlSplitted2
                    string[] htmlSplitted2 = null;
                    htmlSplitted2 = htmlSplitted1[1].Split(new[] { "</td>" }, StringSplitOptions.None);


                    if (htmlSplitted2[0].Contains(puntoSpazio) || htmlSplitted2[0].Contains(spazioPunto) || htmlSplitted2[0].Contains(puntoSingolo))
                    {
                        richTextBox1.Text += id + " | " + source + " | " + htmlSplitted2[0] + "\n";
                    }
                }
                //richTextBox1.Text = ris;
                sr.Close();
            }
        }