国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

HttpHelper

2019-11-17 02:56:10
字體:
供稿:網(wǎng)友

HttpHelper

 public class HttpHelper {        PRivate static CookieContainer _cc = new CookieContainer();        private static WebProxy _proxy;        private static int _delayTime;        private static int _timeout = 120000; // The default is 120000 milliseconds (120 seconds).        private static int _tryTimes = 3; // 默認(rèn)重試3次        private static string _lastUrl = string.Empty;        private static string reqUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; InfoPath.2)";        // 無 referer 的 Get        public static string Get(string url) {            string data = Get(url, _lastUrl);            _lastUrl = url;            return data;        }        /// <summary>        /// Get data from server        /// </summary>        /// <param name="url"></param>        /// <returns>Return content</returns>        public static string Get(string url, string referer)        {            int failedTimes = _tryTimes;            while (failedTimes-- > 0) {                try {                    DelaySomeTime();                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(url));                    req.UserAgent = reqUserAgent;                    req.CookieContainer = _cc;                    req.Referer = referer;                    req.Method = "GET";                    req.Timeout = _timeout;                    if (null != _proxy && null != _proxy.Credentials) {                        req.UseDefaultCredentials = true;                    }                    req.Proxy = _proxy;                                        //接收返回字串                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();                    StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);                    return sr.ReadToEnd();                } catch (Exception e) {                    //TraceLog.Error("HTTP GET Error: " + e.Message);                    //TraceLog.Error("Url: " + url);                }            }            return string.Empty;        }        public static void Download(string url, string localfile)        {            WebClient client = new WebClient();            client.DownloadFile(url, localfile);        }        private static void DelaySomeTime()        {            if (_delayTime > 0) {                Random rd = new Random();                int delayTime = _delayTime * 1000 + rd.Next(1000);                Thread.Sleep(delayTime);            }        }        /// <summary>        /// Set Proxy        /// </summary>        /// <param name="server"></param>        /// <param name="port"></param>        public static void SetProxy(string server, int port, string username, string passWord)        {            if (null != server && port > 0) {                _proxy = new WebProxy(server, port);                if (null != username && null != password) {                    _proxy.Credentials = new NetworkCredential(username, password);                    _proxy.BypassProxyOnLocal = true;                }            }        }        /// <summary>        /// Set delay connect time        /// </summary>        /// <param name="delayTime"></param>        public static void SetDelayConnect(int delayTime)        {            _delayTime = delayTime;        }        /// <summary>        /// Set the timeout for each http request        /// </summary>        /// <param name="timeout"></param>        public static void SetTimeOut(int timeout)        {            if (timeout > 0) {                _timeout = timeout;            }        }        /// <summary>        /// Set the try times for each http request        /// </summary>        /// <param name="times"></param>        public static void SetTryTimes(int times)        {            if (times > 0) {                _tryTimes = times;            }        }    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万宁市| 石门县| 金门县| 平阳县| 固镇县| 辽中县| 平潭县| 沂水县| 南安市| 谢通门县| 富宁县| 阿勒泰市| 同心县| 剑川县| 绵阳市| 陕西省| 阜康市| 那坡县| 灵台县| 德兴市| 高要市| 通海县| 呼图壁县| 杨浦区| 安仁县| 秦皇岛市| 白山市| 西城区| 会宁县| 陇南市| 平谷区| 清水河县| 弋阳县| 康保县| 葵青区| 平和县| 信宜市| 县级市| 墨竹工卡县| 白水县| 封开县|