示例#1
0
        public static string getHtml(string myUrl)
        {
            HttpHelper http = new HttpHelper();
            HttpItem   item = new HttpItem()
            {
                URL               = myUrl, //URL     必需项
                Method            = "get", //URL     可选项 默认为Get
                Expect100Continue = false,
                Allowautoredirect = false,
                KeepAlive         = true,
                UserAgent         = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
                ContentType       = "application/x-www-form-urlencoded",//返回类型    可选项有默认值
                Timeout           = HTML_TIMEOUT,
                //Cookie = cookieString,
            };

            item.Header.Add("AcceptEncoding", "gzip,deflate");
            HttpResult result = http.GetHtml(item);

            NetworkSpeed.increment(Encoding.Default.GetBytes(result.Html).Length);
            string html = result.Html;

            html = html.Replace(@"\s", string.Empty).Replace(@"\r", string.Empty).Replace(@"\n", string.Empty).Replace(@"\f", string.Empty);
            return(html);
        }
示例#2
0
 public static async Task <string> getImg(string imgUrl, string path, string fileName, httpParameter MyHttpParameter)
 {
     return(await Task.Run(() => {
         try {
             var watch = new Stopwatch();
             watch.Start();
             if (path.Equals(""))
             {
                 throw new Exception("未指定保存文件的路径");
             }
             string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/") + 1);
             HttpHelper http = new HttpHelper();
             HttpItem item = new HttpItem()
             {
                 URL = imgUrl,                                                                                       //URL     必需项
                 Method = "get",                                                                                     //URL     可选项 默认为Get
                 Timeout = IMG_TIMEOUT,                                                                              //连接超时时间     可选项默认为100000
                 ReadWriteTimeout = IMG_TIMEOUT,
                 UserAgent = "Mozilla / 5.0(Windows NT 10.0; Win64; x64; rv: 55.0) Gecko / 20100101 Firefox / 55.0", //用户的浏览器类型,版本,操作系统     可选项有默认值
                 ContentType = "text/html",                                                                          //返回类型    可选项有默认值
                 //Cookie = MyHttpParameter.cookie,
                 //Host = MyHttpParameter.host,
                 //Referer = MyHttpParameter.referer,
                 ResultType = ResultType.Byte
             };
             HttpResult result = http.GetHtml(item);
             watch.Stop();
             if (result.ResultByte == null)
             {
                 throw new Exception(result.Html);
             }
             if (result.ResultByte.Length < MIN_IMG_SIZE)
             {
                 throw new Exception("Invalid image size:" + result.ResultByte.Length);
             }
             NetworkSpeed.increment(result.ResultByte.Length);
             NetworkSpeed.addTotalSize(result.ResultByte.Length);
             var milliseconds = watch.ElapsedMilliseconds;//获取请求执行时间
             string saveResult = saveImg(result.ResultByte, path, fileName, imgName);
             if (!saveResult.StartsWith("Error:"))
             {
                 return "Name:" + saveResult + " Time:" + milliseconds + "ms";
             }
             else
             {
                 throw new Exception(saveResult);
             }
         } catch (Exception ex) {
             return (ex.Message.StartsWith("Error:") ? "" : "Error:") + ex.Message + " ImgUrl:" + imgUrl;
         }
     }));
 }