示例#1
0
        /// <summary>
        /// Procède à un download en passant par une méthode POST (passage de paramètre ID !)
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static bool DownloadUrl(string path, string fileName)
        {
            bool retour = false;
            Uri  myStringWebResource = null;

            var    doc        = new HtmlDocument();
            string urlRequest = URLRequest(path);

            if (!string.IsNullOrEmpty(urlRequest))
            {
                doc.LoadHtml(urlRequest);

                var postNodes = doc.DocumentNode.SelectNodes("//input[@name='id']");

                string fileId = postNodes[0].GetAttributeValue("value", "").Trim();

                try
                {
                    ExtWebClient myWebClient = new ExtWebClient();
                    myWebClient.PostParam       = new NameValueCollection();
                    myWebClient.PostParam["id"] = fileId;
                    // Concatenate the domain with the Web resource filename.
                    myStringWebResource = new Uri(baseUri.AbsoluteUri + downloadLink);

                    myWebClient.DownloadFile(myStringWebResource, fileName);

                    // Variation entre 20 et 40 secondes pour tromper l'ennemi :D
                    // et pour palier la limite de download à 1 rom toute les 15 secondes max !
                    System.Threading.Thread.Sleep(20000 + new Random().Next(20000));

                    retour = true;
                }
                catch (Exception)
                {
                    retour = false;
                }
            }
            else
            {
                Console.WriteLine("Problème (Timeout ?)");
            }

            return(retour);
        }
示例#2
0
        private void btnExtWebClientUpload_Click(object sender, EventArgs e)
        {
            Uri uri = new Uri(txtUrl.Text);
            // 準備檔案
            bool done = false;

            using (ExtWebClient wc = new ExtWebClient())
            {
                wc.Encoding = Encoding.UTF8;
                if (!string.IsNullOrEmpty(txtUserAgent.Text))
                {
                    wc.Headers.Add(HttpRequestHeader.UserAgent, txtUserAgent.Text);
                }
                else
                {
                    wc.Headers.Add(HttpRequestHeader.UserAgent, "ExtWebClient");
                }
                wc.UploadMultiFilesCompleted += new EventHandler <UploadMultiFilesCompletedEventArgs>(delegate(object s, UploadMultiFilesCompletedEventArgs se)
                {
                    if (se.Result == UploadMultiFilesCompletedResult.Failed)
                    {
                        WriteLog(Color.Red, "# 發生錯誤, Status Code:{0}/{1}", (int)se.StatusCode, se.StatusCode);
                        if (se.Response != null && se.Response.Length != 0)
                        {
                            WriteLog(Color.Red, Encoding.UTF8.GetString(se.Response));
                        }
                    }
                    else
                    {
                        WriteLog(Color.Green, "# 上傳完成");
                        WriteLog(Color.Green, "# 自伺服器收到:{0}", Encoding.UTF8.GetString(se.Response));
                    }
                    done = true;
                });
                wc.UploadMultiFilesProgressChanged += new EventHandler <UploadMultiFilesProgressChangedEventArgs>(delegate(object s, UploadMultiFilesProgressChangedEventArgs se)
                {
                    this.Invoke(new MethodInvoker(() => { pbPercentage.Value = se.ProgressPercentage; Application.DoEvents(); }));
                });
                NameValueCollection          nvc   = GetNameValueCollection();
                List <ExtWebClient.FileData> files = new List <ExtWebClient.FileData>();
                CJF.Utility.CRC.Crc16        crc   = new CJF.Utility.CRC.Crc16();
                if (!string.IsNullOrEmpty(txtFile1.Text) && File.Exists(txtFile1.Text))
                {
                    files.Add(new ExtWebClient.FileData()
                    {
                        ContentType = ConvUtils.GetContentType(txtFile1.Text),
                        FileName    = txtFile1.Text,
                        KeyName     = "File1"
                    });
                    nvc.Add("File1CRC", crc.ComputeHash(File.ReadAllBytes(txtFile1.Text)).ToHexString(""));
                }
                if (!string.IsNullOrEmpty(txtFile2.Text) && File.Exists(txtFile2.Text))
                {
                    files.Add(new ExtWebClient.FileData()
                    {
                        ContentType = ConvUtils.GetContentType(txtFile2.Text),
                        FileName    = txtFile2.Text,
                        KeyName     = "File2"
                    });
                    nvc.Add("File2CRC", crc.ComputeHash(File.ReadAllBytes(txtFile2.Text)).ToHexString(""));
                }
                wc.UploadMultiFilesAsync(uri, nvc, files.ToArray(), null);
                DateTime now = DateTime.Now;
                while (!done && DateTime.Now.Subtract(now).TotalSeconds <= 60)
                {
                    Application.DoEvents();
                }
            }
        }