예제 #1
0
        private async void OnElementClicked(object sender, RoutedEventArgs e)
        {
            var b = sender as Button;

            switch (b.Name)
            {
            case "SaveButton":
                bool isComplete = await BOkCrawler.DownloadBookAsync(book);

                ShowDownloadToastAsync(isComplete);
                break;

            case "ShareButton":

                ;
                break;

            default:
                break;
            }
        }
예제 #2
0
        public static async Task <bool> DownloadBookAsync(Book book)
        {
            if (book.detailPageLink == null)
            {
                MessageDialog m = new MessageDialog("Couldn't fetch the book info about its detail page!");
                await m.ShowAsync();

                return(false);
            }

            if (book.downloadLink == null)
            {
                book = await BOkCrawler.GetBookInfoFromDetailPage(book.detailPageLink);
            }

            try
            {
                HttpWebRequest req = WebRequest.Create(book.downloadLink) as HttpWebRequest;

                //Headers definition
                req.Headers.Add(HttpRequestHeader.Referer, book.detailPageLink.ToString());
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
                req.Method    = "GET";
                req.Accept    = "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";

                HttpWebResponse response = req.GetResponse() as HttpWebResponse;

                //When the response was ok
                var m = new MessageDialog("You have reached the limit of 5 downloads in 24 hours.");

                // Set the command that will be invoked by default
                m.DefaultCommandIndex = 0;

                // Set the command to be invoked when escape is pressed
                m.CancelCommandIndex = 1;

                // Show the message dialog
                await m.ShowAsync();
            }
            //When the response was 410
            catch (WebException e)
            {
                try
                {
                    Uri d = new Uri(e.Response.Headers.Get("Location").Replace("http", "https"));


                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(d);

                    //Headers definition
                    req.AllowAutoRedirect = true;

                    req.Method    = "GET";
                    req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
                    req.Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
                    req.Host      = "dl123.zlibcdn.com";

                    WebResponse response = await req.GetResponseAsync();

                    string contentDisposition = response.Headers.Get("Content-Disposition");

                    string pattern = "filename=\"(.+?)\"$";

                    string fileName = Regex.Matches(contentDisposition, pattern).First().Groups[1].ToString();

                    StorageFile destinationFile;

                    if (BookView.DownloadPath == null)
                    {
                        destinationFile = await DownloadsFolder.CreateFileAsync(GetSafeFilename(fileName), CreationCollisionOption.GenerateUniqueName);

                        BookView.DownloadPathString = Path.GetDirectoryName(destinationFile.Path);
                    }
                    else
                    {
                        destinationFile = await BookView.DownloadPath.CreateFileAsync(GetSafeFilename(fileName), CreationCollisionOption.GenerateUniqueName);
                    }

                    Stream s = response.GetResponseStream();
                    await Task.Run(() => FileIO.WriteBytesAsync(destinationFile, ReadStream(s)));

                    return(true);
                }
                catch (WebException)
                {
                    MessageDialog m = new MessageDialog("Couldn't download the file!");
                    await m.ShowAsync();
                }
            }
            return(false);
        }