Ejemplo n.º 1
0
        public static string GetPageString(string url)
        {
            string res = null;
            int    t   = 0;

            do
            {
                t++;
                try
                {
                    var request = WebRequest.Create(url) as HttpWebRequest;
                    request.Method    = "GET";
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
                    using (var response = request.GetResponse())
                    {
                        using (var resStream = response.GetResponseStream())
                        {
                            using (var sr = new StreamReader(resStream, Encoding.GetEncoding("GBK")))
                            {
                                res = sr.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (t == 2)
                    {
                        LogHelp.Log("=================================页面获取失败:" + url, true);
                    }
                    res = null;
                }
            } while ((res == null || res.Length == 0) && t <= 2);
            if (res == null || res.Length == 0)
            {
                MyDbCOntextHelp.AddErroeProcess(new Data.ErroeProcess
                {
                    CreateTime = DateTime.Now,
                    Type       = 3,
                    Url        = url
                });
            }
            return(res);
        }
Ejemplo n.º 2
0
        public static async Task <string> GetPageStringAsync(string url)
        {
            string res = null;
            int    t   = 0;

            do
            {
                t++;
                try
                {
                    var respMsg = await client.GetAsync(url);

                    using (var resStream = await respMsg.Content.ReadAsStreamAsync())
                    {
                        using (var sr = new StreamReader(resStream, Encoding.GetEncoding("GBK")))
                        {
                            res = await sr.ReadToEndAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (t == 5)
                    {
                        LogHelp.Log("=================================页面获取失败:" + url, true);
                        LogHelp.Log("=================================" + ex.Message);
                        LogHelp.Log("=================================", true);
                    }
                    res = null;
                }
            } while ((res == null || res.Length == 0) && t <= 5);
            if (res == null || res.Length == 0)
            {
                MyDbCOntextHelp.AddErroeProcess(new Data.ErroeProcess
                {
                    CreateTime = DateTime.Now,
                    Type       = 3,
                    Url        = url
                });
            }
            return(res);
        }
Ejemplo n.º 3
0
        public static async Task <bool> DownloadImgAsync(string url, string path)
        {
            bool res = false;
            int  t   = 0;

            do
            {
                t++;
                try
                {
                    var fileName = Path.Combine(path, Path.GetFileName(url));//如果文件存在,则不需要再接受,直接返回
                    if (File.Exists(fileName) && new FileInfo(fileName).Length != 0)
                    {
                        return(true);
                    }
                    var respMsg = await client.GetAsync(url);

                    using (var resStream = await respMsg.Content.ReadAsStreamAsync())
                    {
                        //如果文件存在,则不需要再接受,直接返回
                        if (File.Exists(fileName) && new FileInfo(fileName).Length == respMsg.Content.Headers.ContentLength)
                        {
                            return(true);
                        }
                        using (var FileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 4096, true))
                        {
                            byte[] buf  = new byte[1024 * 1024];
                            int    blen = 0;
                            do
                            {
                                blen = await resStream.ReadAsync(buf, 0, buf.Length);

                                if (blen != 0)
                                {
                                    await FileStream.WriteAsync(buf, 0, blen);
                                }
                            } while (blen != 0);
                        }
                        res = true;
                    }
                }
                catch (Exception ex)
                {
                    if (t == 2)
                    {
                        LogHelp.Log("=================================图片获取失败:" + url, true);
                    }
                    res = false;
                }
            } while (res == false && t <= 2);
            if (!res)
            {
                MyDbCOntextHelp.AddErroeProcess(new Data.ErroeProcess
                {
                    CreateTime = DateTime.Now,
                    Type       = 1,
                    Url        = url
                });
            }
            return(res);
        }
Ejemplo n.º 4
0
        public static bool DownloadImg(string url, string path)
        {
            bool res = false;
            int  t   = 0;

            do
            {
                t++;
                try
                {
                    var fileName = Path.Combine(path, Path.GetFileName(url));//如果文件存在,则不需要再接受,直接返回
                    if (File.Exists(fileName) && new FileInfo(fileName).Length != 0)
                    {
                        return(true);
                    }
                    var request = WebRequest.Create(url) as HttpWebRequest;
                    request.Method    = "GET";
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
                    using (var response = request.GetResponse())
                    {
                        using (var resStream = response.GetResponseStream())
                        {
                            //如果文件存在,则不需要再接受,直接返回
                            if (File.Exists(fileName) && new FileInfo(fileName).Length == response.ContentLength)
                            {
                                return(true);
                            }
                            using (var FileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                            {
                                byte[] buf  = new byte[8192];
                                int    blen = 0;
                                do
                                {
                                    blen = resStream.Read(buf, 0, buf.Length);
                                    if (blen != 0)
                                    {
                                        FileStream.Write(buf, 0, blen);
                                    }
                                } while (blen != 0);
                            }
                            res = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (t == 2)
                    {
                        LogHelp.Log("=================================图片获取失败:" + url, true);
                    }
                    res = false;
                }
            } while (res == false && t <= 2);
            if (!res)
            {
                MyDbCOntextHelp.AddErroeProcess(new Data.ErroeProcess
                {
                    CreateTime = DateTime.Now,
                    Type       = 1,
                    Url        = url
                });
            }
            return(res);
        }