Beispiel #1
0
        /// <summary>
        /// 执行HTTP GET请求。
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="textParams">请求文本参数</param>
        /// <param name="headerParams">请求头部参数</param>
        /// <returns>HTTP响应</returns>
        public string DoGet(string url, IDictionary <string, string> textParams, IDictionary <string, string> headerParams, out string proxyIp, bool useProxy = false)
        {
            if (textParams != null && textParams.Count > 0)
            {
                url = BuildRequestUrl(url, textParams);
            }

            HttpWebRequest req = GetWebRequest(url, "GET", headerParams, out proxyIp, useProxy);

            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            HttpWebResponse rsp;

            try
            {
                rsp = req.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                ProxyIpHelper.RemoveIp(proxyIp);

                throw new BaseException($"{ex.Message}{proxyIp}");
            }
            Encoding encoding = GetResponseEncoding(rsp);

            return(GetResponseAsString(rsp, encoding));
        }
Beispiel #2
0
        public HttpWebRequest GetWebRequest(string url, string method, IDictionary <string, string> headerParams, out string proxyIp, bool useProxy = false)
        {
            proxyIp = "";

            HttpWebRequest req = null;

            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                if (this._ignoreSSLCheck)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(TrustAllValidationCallback);
                }

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

                req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                req = (HttpWebRequest)WebRequest.Create(url);
            }

            if (useProxy)
            {
                proxyIp = ProxyIpHelper.GetProxyIp();
                WebProxy proxyObject = new WebProxy($"http://{proxyIp}/", true);
                req.ProtocolVersion = HttpVersion.Version10;
                req.Proxy           = proxyObject;
            }

            if (headerParams != null && headerParams.Count > 0)
            {
                foreach (string key in headerParams.Keys)
                {
                    req.Headers.Add(key, headerParams[key]);
                }
            }

            req.ServicePoint.Expect100Continue = false;
            req.Method    = method;
            req.KeepAlive = true;
            //req.UserAgent = "top-sdk-net";
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
            //req.Accept = "text/xml,text/javascript";
            req.Accept           = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            req.Timeout          = this._timeout;
            req.ReadWriteTimeout = this._readWriteTimeout;

            return(req);
        }
Beispiel #3
0
 public Task Execute(IJobExecutionContext context)
 {
     if (!isRun)
     {
         return(Task.Run(() =>
         {
             isRun = true;
             Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 定时任务开始执行");
             ProxyIpHelper proxyhelper = new ProxyIpHelper();
             proxyhelper.start();
             Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 定时任务执行完毕");
             isRun = false;
         }));
     }
     else
     {
         return(Task.Run(() =>
         {
         }));
     }
 }