示例#1
0
        /// <summary>
        /// 站点登录处理 通过IE
        /// </summary>
        /// <param name="imageSite">站点</param>
        /// <param name="cookie">站点内部cookie, 将返回登录后的cookie, 登录失败为string.Empty</param>
        /// <param name="LoggedFlags">登录成功页面验证字符, 多个字符用|分隔; 无需验证请置null</param>
        /// <param name="Sweb">站点内部SessionClient</param>
        /// <param name="shc">站点内部SessionHeadersCollection</param>
        /// <param name="PageString">返回验证登录时的页面HTML</param>
        /// <returns></returns>
        public static bool LoginSite(ImageSite imageSite, ref string cookie, string LoggedFlags,
                                     ref SessionClient Sweb, ref SessionHeadersCollection shc, ref string pageString)
        {
            string tmp_cookie = CookiesHelper.GetIECookies(imageSite.SiteUrl);
            bool   result     = !string.IsNullOrWhiteSpace(tmp_cookie) && tmp_cookie.Length > 3;

            if (result)
            {
                shc.Timeout = shc.Timeout * 2;
                shc.Set("Cookie", tmp_cookie);
                try
                {
                    pageString = Sweb.Get(imageSite.SiteUrl, Mainproxy, shc);
                    result     = !string.IsNullOrWhiteSpace(pageString);

                    if (result && LoggedFlags != null)
                    {
                        string[] LFlagsArray = LoggedFlags.Split('|');
                        foreach (string Flag in LFlagsArray)
                        {
                            result &= pageString.Contains(Flag);
                        }
                    }

                    cookie = result ? tmp_cookie : cookie;
                }
                catch
                {
                    //有cookie访问时发生超时之类的错误 还是作为用户登录状态 返回true
                    cookie = string.Empty;
                }
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// do in a separate thread
        /// 下载缩略图线程
        /// </summary>
        /// <param name="page"></param>
        /// <param name="count"></param>
        /// <param name="word"></param>
        public void PreFetchPage(int page, int count, string word, ImageSite site)
        {
            (new Thread(new ThreadStart(() =>
            {
                try
                {
                    preFetchedPage = site.GetPageString(page, count, word, proxy);
                    prePage = page;
                    preCount = count;
                    preWord = word;
                    preSite = site;
                    List <Img> imgs = site.GetImages(preFetchedPage, proxy);

                    //获得所有图片列表后反馈得到的数量
                    PreListLoaded(imgs.Count, null);
                    if (imgs.Count < 1)
                    {
                        return;
                    }

                    SessionClient sweb = new SessionClient();
                    SessionHeadersCollection shc = new SessionHeadersCollection();
                    shc.Accept = null;
                    shc.ContentType = SessionHeadersValue.ContentTypeAuto;
                    shc.Add("Accept-Ranges", "bytes");
                    shc.Referer = site.Referer;

                    imgs = site.FilterImg(imgs, MainWindow.MainW.MaskInt, MainWindow.MainW.MaskRes,
                                          MainWindow.MainW.LastViewed, MainWindow.MainW.MaskViewed, true, false);

                    //预加载缩略图
                    foreach (HttpWebRequest req1 in imgReqs)
                    {
                        if (req1 != null)
                        {
                            req1.Abort();
                        }
                    }
                    preFetchedImg.Clear();
                    imgReqs.Clear();

                    //prefetch one by one
                    int cacheCount = CachedImgCount < imgs.Count ? CachedImgCount : imgs.Count;
                    for (int i = 0; i < cacheCount; i++)
                    {
                        WebResponse res = sweb.GetWebResponse(imgs[i].PreviewUrl, proxy, 9000, shc);
                        System.IO.Stream str = res.GetResponseStream();

                        if (!preFetchedImg.ContainsKey(imgs[i].PreviewUrl))
                        {
                            preFetchedImg.Add(imgs[i].PreviewUrl, MainWindow.MainW.CreateImageSrc(str));
                        }
                    }
                }
                catch
                {
                    //Console.WriteLine("useless");
                }
            }))).Start();
        }
示例#3
0
        /// <summary>
        /// 站点登录处理 通过IE
        /// </summary>
        /// <param name="imageSite">站点</param>
        /// <param name="cookie">站点内部cookie, 将返回登录后的cookie, 登录失败为string.Empty</param>
        /// <param name="LoggedFlags">登录成功页面验证字符, 多个字符用|分隔; 无需验证请置null</param>
        /// <param name="Sweb">站点内部SessionClient</param>
        /// <param name="shc">站点内部SessionHeadersCollection</param>
        /// <returns></returns>
        public static bool LoginSite(ImageSite imageSite, ref string cookie, string LoggedFlags, ref SessionClient Sweb, ref SessionHeadersCollection shc)
        {
            string NullPageString = string.Empty;

            return(LoginSite(imageSite, ref cookie, LoggedFlags, ref Sweb, ref shc, ref NullPageString));
        }
示例#4
0
        /// <summary>
        /// 下载,另一线程
        /// </summary>
        /// <param name="o"></param>
        private void Download(object o)
        {
            DownloadTask  task = (DownloadTask)o;
            FileStream    fs   = null;
            Stream        str  = null;
            SessionClient sc   = new SessionClient();

            System.Net.WebResponse res = null;
            double       downed        = 0;
            DownloadItem item          = downloadItemsDic[task.Url];

            try
            {
                res = sc.GetWebResponse(
                    task.Url,
                    MainWindow.WebProxy,
                    task.NeedReferer
                    );

                /////////开始写入文件
                str = res.GetResponseStream();
                byte[] bytes = new byte[5120];
                fs = new FileStream(task.SaveLocation + DLEXT, FileMode.Create);

                int      bytesReceived = 0;
                DateTime last          = DateTime.Now;
                int      osize         = str.Read(bytes, 0, bytes.Length);
                downed = osize;
                while (!task.IsStop && osize > 0)
                {
                    fs.Write(bytes, 0, osize);
                    bytesReceived += osize;
                    DateTime now   = DateTime.Now;
                    double   speed = -1;
                    if ((now - last).TotalSeconds > 0.6)
                    {
                        speed  = downed / (now - last).TotalSeconds / 1024.0;
                        downed = 0;
                        last   = now;
                    }
                    Dispatcher.Invoke(new DownloadHandler(web_DownloadProgressChanged),
                                      res.ContentLength, bytesReceived / (double)res.ContentLength * 100.0, task.Url, speed);
                    osize   = str.Read(bytes, 0, bytes.Length);
                    downed += osize;
                }
            }
            catch (Exception ex)
            {
                //Dispatcher.Invoke(new UIdelegate(delegate(object sender) { StopLoadImg(re.Key, re.Value); }), "");
                task.IsStop = true;
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    //下载失败
                    if (downloadItemsDic.ContainsKey(task.Url))
                    {
                        item.StatusE = DLStatus.Failed;
                        item.Size    = "下载失败";
                        WriteErrText(task.Url);
                        WriteErrText(task.SaveLocation);
                        WriteErrText(ex.Message + "\r\n");

                        try
                        {
                            if (fs != null)
                            {
                                fs.Close();
                            }
                            if (str != null)
                            {
                                str.Close();
                            }
                            if (res != null)
                            {
                                res.Close();
                            }

                            File.Delete(task.SaveLocation + DLEXT);
                            DelDLItemNullDirector(item);
                        }
                        catch { }
                    }
                }));
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (str != null)
                {
                    str.Close();
                }
                if (res != null)
                {
                    res.Close();
                }
            }

            if (task.IsStop)
            {
                //任务被取消
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    if (downloadItemsDic.ContainsKey(task.Url))
                    {
                        if (!dlerrtxt.Contains(item.Size))
                        {
                            item.StatusE = DLStatus.Cancel;
                        }
                    }
                }));

                try
                {
                    File.Delete(task.SaveLocation + DLEXT);
                    DelDLItemNullDirector(item);
                }
                catch { }
            }
            else
            {
                //下载成功完成
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    try
                    {
                        //DownloadTask task1 = obj as DownloadTask;

                        //判断完整性
                        if (!item.NoVerify && 100 - item.Progress > 0.001)
                        {
                            task.IsStop  = true;
                            item.StatusE = DLStatus.Failed;
                            item.Size    = "下载未完成";
                            try
                            {
                                File.Delete(task.SaveLocation + DLEXT);
                                DelDLItemNullDirector(item);
                            }
                            catch { }
                        }
                        else
                        {
                            //修改后缀名
                            File.Move(task.SaveLocation + DLEXT, task.SaveLocation);

                            item.Progress = 100.0;
                            item.StatusE  = DLStatus.Success;
                            //downloadItemsDic[task.Url].Size = (downed > 1048576
                            //? (downed / 1048576.0).ToString("0.00MB")
                            //: (downed / 1024.0).ToString("0.00KB"));
                            numSaved++;
                        }
                    }
                    catch { }
                }));
            }

            //下载结束
            Dispatcher.Invoke(new VoidDel(delegate()
            {
                webs.Remove(task.Url);
                RefreshList();
            }));
        }