Exemplo n.º 1
0
        MemoryStream DoRequest(Uri uri, DownModel dm)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);

            webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
            webRequest.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
            webRequest.Headers.Add("Cache-Control", "max-age=0");

            webRequest.KeepAlive = true;
            webRequest.Headers.Add("DNT", "1");
            webRequest.Host = "mt0.google.cn";
            //webRequest.Headers.Add("Pragma", "no-cache");
            webRequest.UserAgent =
                @"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
            Chrome/28.0.1500.95 Safari/537.36 SE 2.X MetaSr 1.0";

            //webRequest.Headers.Add("X-Client-Data", "CJW2yQEIpbbJAQiptskBCMS2yQEI7ojKAQ==");
            WebResponse webResponse = webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            MemoryStream ms = new MemoryStream();

            byte[] buffBytes = new byte[1000];
            int len = 0;
            while (true)
            {
                len = stream.Read(buffBytes, 0, 1000);
                if (len > 0)
                {
                    ms.Write(buffBytes, 0, len);
                }
                else
                {
                    break;
                }
            }
            return ms;
        }
Exemplo n.º 2
0
        void DownCompleteProcess(DownModel dm)
        {
            try
            {
                downcount++;
                if (downcount % 20 == 0)
                {
                    if (downcount % 500 == 0)
                    {
                        if (dm.x > saveInfo.stopx)
                        {
                            saveInfo.stopx = dm.x;
                        }
                        if (dm.y > saveInfo.stopy)
                        {
                            saveInfo.stopy = dm.y;
                        }
                        //File.WriteAllText(saveInfo.savepath + "/saveinfo.inf",
                        //saveInfo.level + "," + saveInfo.stopx + "," + saveInfo.stopy);
                    }

                    OnPrecessEvent(downcount);
                }

                //完成下载 触发事件
                if (urls.Count > 0 && NextQuery())
                {

                }
                else
                {
                    lock (tc)
                    {
                        if (tc.allThreadNumber > 0)
                        {
                            tc.allThreadNumber--;
                            OnPrecessStatuEvent("线程完成 剩余:" + tc.allThreadNumber);

                            if (dm.x > saveInfo.stopx)
                            {
                                saveInfo.stopx = dm.x;
                            }
                            if (dm.y > saveInfo.stopy)
                            {
                                saveInfo.stopy = dm.y;
                            }

                            if (tc.allThreadNumber == 0)
                            {
                                OnPrecessStatuEvent("线程全部完成 触发完成事件");
                                downisRun = false;
                                //File.WriteAllText(saveInfo.savepath + "/saveinfo.inf",
                                //    saveInfo.level + "," + saveInfo.stopx + "," + saveInfo.stopy);
                                OnCompleteEvent(downcount, isMulSave);
                            }
                        }
                        else
                        {
                            OnPrecessStatuEvent("线程全部完成");
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                DownStop();
                onprecessStatu(ex.Message + " " + ex.StackTrace);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建队列方法
        /// </summary>
        /// <param name="startx"></param>
        /// <param name="starty"></param>
        /// <param name="endx"></param>
        /// <param name="endy"></param>
        /// <param name="zoom"></param>
        /// <param name="mt"></param>
        private void CreateQueue(int startx, int starty, int endx, int endy,
            int zoom, MapType mt,
            int passstartx, int passstarty, int passendx, int passendy)
        {
            try
            {
                if (passendx == 0)
                {
                    passendx = endx;
                }
                if (passendy == 0)
                {
                    passendy = endy;
                }
                inputqueuecount = 0;

                while (addqueueisRun)
                {
                    if (passstartx <= endx || passstarty <= endy)
                    {
                        if (urls.Count < 100)
                        {
                            OnPrecessStatuEvent("队列不足200 继续创建。" + urls.Count);

                            onprecessStatu("创建队列 开始于" + passstartx + "X" + passstarty);
                            Zoom zm = zoomlevel.GetLevel(zoom, mt);
                            if (endx > zm.maxX || endy > zm.maxY)
                            {
                                return;
                            }
                            string url = "";
                            int partnum = 0;
                            string partDoc = "";

                            lock (urls)
                            {

                                for (int x = startx; x <= endx; x++)
                                {
                                    for (int y = starty; y <= endy; y++)
                                    {
                                        if (passstartx > 0)
                                        {
                                            x = passstartx;
                                            passstartx = 0;
                                        }
                                        if (passstarty > 0)
                                        {
                                            y = passstarty;
                                            passstarty = 0;
                                        }

                                        url = zm.url.Replace("@X", x.ToString()).Replace("@Y", y.ToString());
                                        DownModel dm = new DownModel();
                                        dm.Url = url;

                                        dm.x = x;
                                        dm.y = y;
                                        partnum = (int)Math.Floor((double)((double)(x + 1) * (double)(zm.maxY + 1) / 1000));
                                        //计算分部文件夹编号

                                        partDoc = saveInfo.savepath + "/" + _zoom + "/" + _zoom + "_" + partnum;
                                        if (!Directory.Exists(partDoc))
                                        {
                                            Directory.CreateDirectory(partDoc);
                                        }

                                        dm.Fielname = partDoc + "/" + _zoom + "_" + x + "_" + y + ".png";
                                        urls.Enqueue(dm);
                                        inputqueuecount++;

                                        if (x == passendx && y == passendy)
                                        {

                                        }
                                        if (x == passendx && y == passendy)
                                        {
                                            //开始数大于结束数 停止队列
                                            passstartx = endx + 1;
                                            passstarty = endy + 1;
                                            x = endx + 1;//设置x值大于截止值 则下次循环将不执行
                                            y = endy + 1;
                                            onprecessStatu("队列 x y 等于结束数 " + passendx + "x" + passendy);
                                            if (inputqueuecount <= 100 && downisRun == false)
                                            {
                                                BeginDown();//开始下载
                                            }
                                        }
                                        else
                                        {

                                            if (urls.Count > 100 && downisRun == false)
                                            {
                                                BeginDown();//开始下载
                                            }

                                            if (urls.Count > 200)
                                            {
                                                passstartx = x;
                                                passstarty = y;
                                                onprecessStatu("队列 " + urls.Count + " 停止入列。" + passstartx + "X" + passstarty + " 入列总数" + inputqueuecount);
                                                x = endx + 1;//设置x值大于截止值 则下次循环将不执行
                                                y = endy + 1;
                                            }

                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Thread.Sleep(1000);

                        }
                    }
                    else
                    {
                        addqueueisRun = false;
                        onprecessStatu("队列 创建完成 停止入列。" + passstartx + "X" + passstarty + " 入列总数" + inputqueuecount);
                    }
                }
            }
            catch (Exception ex)
            {

                onprecessStatu(ex.Message + " " + ex.StackTrace);
            }
        }