Beispiel #1
0
        /// <summary>
        /// Get请求网站
        /// </summary>
        /// <param name="Url">地址</param>
        /// <returns>网站返回的数据</returns>
        public static string GetHtml(string Url)
        {
            string sRslt = null;

            LogTool.WriteLogDebug(typeof(WebTool), $"正在GET {Url} ");
            WebResponse    oWebRps = null;
            HttpWebRequest rq      = (HttpWebRequest)WebRequest.Create(Url);

            rq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
            rq.Method    = "GET";
            rq.Timeout   = 5000;
            try
            {
                oWebRps = rq.GetResponse();
            }
            catch (Exception e)
            {
                sRslt = "ERROR:" + e.Message;
                LogTool.WriteLogError(typeof(WebTool), $"GET {Url} 时发生错误!", e);
            }
            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return(sRslt);
        }
 public void CreateDataFile()
 {
     try
     {
         HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest;
         Request.Referer = "http://pan.baidu.com/disk/home";
         if (Cookies != null)
         {
             Cookie ck = new Cookie("BDUSS", Cookies.BDUSS);
             ck.Domain = ".baidu.com";
             Request.CookieContainer = new CookieContainer();
             Request.CookieContainer.Add(ck);
             ck        = new Cookie("pcsett", Cookies.PCSETT);
             ck.Domain = ".baidu.com";
             Request.CookieContainer.Add(ck);
         }
         HttpWebResponse Response = Request.GetResponse() as HttpWebResponse;
         if (!File.Exists(DownloadPath + ".dcj"))
         {
             LogTool.WriteLogDebug(typeof(HttpDownload), "正在创建文件: " + DownloadPath + ".dcj");
             DownloadInfo info = new DownloadInfo
             {
                 ContentLength = Response.ContentLength,
                 BlockLength   = Response.ContentLength / ThreadNum,
                 DownloadUrl   = Url,
                 Cookies       = Cookies
             };
             info.init(DownloadPath + ".dcj");
         }
         Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj"));
     }
     catch (Exception ex)
     {
         LogTool.WriteLogError(typeof(HttpDownload), "创建数据文件出现错误", ex);
         MessageBox.Show("创建数据文件时出现错误: " + ex.Message);
         File.Delete(DownloadPath + ".dcj");
     }
 }
 /// <summary>
 /// 开始下载
 /// </summary>
 public void Start()
 {
     try
     {
         Downloading = true;
         Stoped      = false;
         HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest;
         Request.Referer = "http://pan.baidu.com/disk/home";
         //第一次下载设置Cookies
         if (Cookies != null)
         {
             Cookie ck = new Cookie("BDUSS", Cookies.BDUSS);
             ck.Domain = ".baidu.com";
             Request.CookieContainer = new CookieContainer();
             Request.CookieContainer.Add(ck);
             ck        = new Cookie("pcsett", Cookies.PCSETT);
             ck.Domain = ".baidu.com";
             Request.CookieContainer.Add(ck);
         }
         //改为获取Response之前读入数据文件,这样就能读取到Cookies了
         if (File.Exists(DownloadPath + ".dcj"))
         {
             Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj"));
             if (Info.Cookies != null)
             {
                 Cookie ck = new Cookie("BDUSS", Info.Cookies.BDUSS);
                 ck.Domain = ".baidu.com";
                 Request.CookieContainer = new CookieContainer();
                 Request.CookieContainer.Add(ck);
                 ck        = new Cookie("pcsett", Info.Cookies.PCSETT);
                 ck.Domain = ".baidu.com";
                 Request.CookieContainer.Add(ck);
             }
         }
         HttpWebResponse Response = Request.GetResponse() as HttpWebResponse;
         if (!File.Exists(DownloadPath + ".dcj"))
         {
             Info = new DownloadInfo
             {
                 ContentLength = Response.ContentLength,
                 BlockLength   = Response.ContentLength / ThreadNum,
                 DownloadUrl   = Url,
                 Cookies       = Cookies
             };
             Info.init(DownloadPath + ".dcj");
         }
         if (Info.Completed)
         {
             Downloading = false;
             Completed   = true;
             Percentage  = 100F;
             return;
         }
         if (!File.Exists(DownloadPath))
         {
             LogTool.WriteLogDebug(typeof(HttpDownload), "正在创建文件: " + DownloadPath);
             FileStream Stream = new FileStream(DownloadPath, FileMode.CreateNew);
             Stream.SetLength(Response.ContentLength);
             Stream.Close();
         }
         Threads = new DownloadThread[Info.DownloadBlockList.Count];
         for (int i = 0; i < Info.DownloadBlockList.Count; i++)
         {
             DownloadBlock Block = JsonConvert.DeserializeObject <DownloadBlock>(Info.DownloadBlockList[i].ToString());
             Threads[i] = new DownloadThread
             {
                 ID          = i,
                 DownloadUrl = Url,
                 Path        = DownloadPath,
                 Block       = Block,
                 Info        = Info
             };
             Threads[i].ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent;
         }
         new Thread(a).Start();
     }
     catch (Exception ex)
     {
         Downloading = false;
         Stoped      = true;
         LogTool.WriteLogError(typeof(HttpDownload), "创建下载任务出现错误", ex);
     }
 }