zoukankan      html  css  js  c++  java
  • 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; // 默认重试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;
                }
            }
        }
  • 相关阅读:
    [转帖]ExtJs与服务器的交互(一)
    Ext_两种处理服务器端返回值的方式
    命令行调用dubbo远程服务
    【转】Lombok Pojo默认初始值问题
    【转】Python之可变参数,*参数,**参数,以及传入*参数,进行解包
    Python获取并输出当前日期时间
    idea上 实现了Serializable接口,要自动生成serialVersionUID的方法
    mac下python2.x和python3.x的安装方法和升级方法/卸载
    工程代码不编译src的java目录下的xml文件问题及解决
    Spring Aop织入点语法
  • 原文地址:https://www.cnblogs.com/wlwjc/p/3953027.html
Copyright © 2011-2022 走看看