专栏首页c#Winform自定义控件系列c# 查询本机可用的代理ip

c# 查询本机可用的代理ip

现在有很多网站都提供免费的代理ip,但是你会发现很多网站显示的可以用的 ,在自己电脑上是用不了,写个小代码提取出自己电脑上可以用的代理,没什么技术含量,只是为了记录一下

 string strUrl = "https://www.xicidaili.com/nt/";
            HttpHelper httpProxy = new HttpHelper();
            for (int i = 1; i <= 728; i++)
            {
                string strHtml = httpProxy.Get_Request(strUrl + i, httpProxy.cookie, "www.xicidaili.com", "https://www.xicidaili.com/nt/", blnHttps: true, strAccept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", strUserAgent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");
                HtmlAgilityPack.HtmlDocument docList = new HtmlAgilityPack.HtmlDocument();
                docList.LoadHtml(strHtml);
                var trListNodes = docList.DocumentNode.SelectNodes("//*[@id=\"ip_list\"]/tr");
                for (int j = 1; j < trListNodes.Count; j++)
                {
                    var trNode = trListNodes[j];
                    string strIp = trNode.SelectSingleNode("td[2]").InnerText.Trim();
                    string strPort = trNode.SelectSingleNode("td[3]").InnerText.Trim();
                    int intPort = int.Parse(strPort);
                    try
                    {
                        WebProxy proxyObject = new WebProxy(strIp, intPort);// port为端口号 整数型
                        HttpWebRequest Req = WebRequest.Create("http://www.bookschina.com") as HttpWebRequest;
                        Req.Proxy = proxyObject; //设置代理
                        Req.Timeout = 2000;   //超时
                        DateTime dt = DateTime.Now;
                        var Resp = (HttpWebResponse)Req.GetResponse();
                        Encoding bin = Encoding.GetEncoding("gb2312");
                        StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin);
                        string str = sr.ReadToEnd();
                        sr.Close();
                        sr.Dispose();
                        var time = (DateTime.Now - dt).TotalMilliseconds;
                        Console.WriteLine(strIp + "   " + intPort + " " + time);
                    }
                    catch
                    { }
                }

            }

HttpHelper

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace TestProxy
{
    public class HttpHelper
    {
        public CookieContainer cookie;
        public HttpHelper()
        {
            cookie = new CookieContainer();
        }
        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受     
        }
        public string Get_Request(
            string strUrl,
            CookieContainer _cookie = null,
            string strHost = "",
            string strRefer = "",
            string strOrigin = "",
            Dictionary<string, string> lstHeads = null,
            string strEncoding = "utf-8",
            string strContentType = "",
            string strAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            string strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
            bool blnAllowAutoRedirect = true,
            int intTimeout = 1000 * 30,
            bool blnHttps = false,
            System.Net.WebProxy proxy = null)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Accept = strAccept;
            request.Timeout = intTimeout;
            request.Method = "GET";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.UserAgent = strUserAgent;
            request.AllowAutoRedirect = blnAllowAutoRedirect;
            if (blnHttps)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version10;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            }
            if (!string.IsNullOrEmpty(strContentType))
            {
                request.ContentType = strContentType;
            }
            if (_cookie != null)
            {
                request.CookieContainer = _cookie;
            }
            if (!string.IsNullOrEmpty(strHost))
            {
                request.Host = strHost;
            }
            if (!string.IsNullOrEmpty(strRefer))
            {
                request.Referer = strRefer;
            }
            if (!string.IsNullOrEmpty(strOrigin))
            {
                request.Headers.Add("Origin", strOrigin);
            }
            if (lstHeads != null && lstHeads.Count > 0)
            {
                foreach (var item in lstHeads)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            
            if (proxy != null)
                request.Proxy = proxy;
            response = (HttpWebResponse)request.GetResponse();
            var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding));
            string strResult = sr.ReadToEnd();
            sr.Close();
            request.Abort();
            response.Close();
            return strResult;

        }

        public string POST_Request(
            string strUrl,
            string postDataStr,
            CookieContainer _cookie = null,
            string strHost = "",
            string strRefer = "",
            string strOrigin = "",
            Dictionary<string, string> lstHeads = null,
            string strEncoding = "utf-8",
            string strContentType = "",
            string strAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            string strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
            bool blnAllowAutoRedirect = true,
            int intTimeout = 1000 * 30,
            bool blnHttps = false,
            System.Net.WebProxy proxy = null)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Accept = strAccept;
            request.Timeout = intTimeout;
            request.Method = "POST";
            request.Host = strHost;
            request.UserAgent = strUserAgent;
            if (_cookie != null)
            {
                request.CookieContainer = _cookie;
            }
            if (blnHttps)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version10;

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            }
            request.AllowAutoRedirect = blnAllowAutoRedirect;
            if (!string.IsNullOrEmpty(strContentType))
            {
                request.ContentType = strContentType;
            }
            if (!string.IsNullOrEmpty(strOrigin))
            {
                request.Headers.Add("Origin", strOrigin);
            }
            if (!string.IsNullOrEmpty(strRefer))
            {
                request.Referer = strRefer;
            }
            if (!string.IsNullOrEmpty(strHost))
            {
                request.Host = strHost;
            }
            if (lstHeads != null && lstHeads.Count > 0)
            {
                foreach (var item in lstHeads)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            if (!string.IsNullOrEmpty(postDataStr))
            {
                request.ContentLength = postDataStr.Length;
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream);
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();
            }
            if (proxy != null)
                request.Proxy = proxy;
            response = (HttpWebResponse)request.GetResponse();
            var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding));
            string strResult = sr.ReadToEnd();
            sr.Close();
            request.Abort();
            response.Close();
            return strResult;
        }

        public string DownloadFile(
            string strURLAddress,
            string strPath,
            CookieContainer _cookie = null,
            string strHost = "",
            string strRefer = "",
            string strOrigin = "",
            Dictionary<string, string> lstHeads = null,
            string strAccept = "",
            string strUserAgent = "",
            bool blnHttps = false,
            System.Net.WebProxy proxy = null)
        {
            try
            {
                // 设置参数
                HttpWebRequest request = WebRequest.Create(strURLAddress) as HttpWebRequest;
                if (!string.IsNullOrEmpty(strAccept))
                {
                    request.Accept = strAccept;
                }
                if (blnHttps)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request.ProtocolVersion = HttpVersion.Version10;

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                }
                if (!string.IsNullOrEmpty(strUserAgent))
                {
                    request.UserAgent = strUserAgent;
                }
                if (_cookie != null)
                {
                    request.CookieContainer = _cookie;
                }
                if (!string.IsNullOrEmpty(strOrigin))
                {
                    request.Headers.Add("Origin", strOrigin);
                }
                if (!string.IsNullOrEmpty(strRefer))
                {
                    request.Referer = strRefer;
                }
                if (!string.IsNullOrEmpty(strHost))
                {
                    request.Host = strHost;
                }
                if (lstHeads != null && lstHeads.Count > 0)
                {
                    foreach (var item in lstHeads)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }
                if (proxy != null)
                    request.Proxy = proxy;
                request.Timeout = 2000;
                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                string strReceivePath = string.Empty;

                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                Stream responseStream = response.GetResponseStream();
                //创建本地文件写入流
                Stream stream = new FileStream(strPath, FileMode.Create);
                byte[] bArr = new byte[1024];
                int size = responseStream.Read(bArr, 0, (int)bArr.Length);
                while (size > 0)
                {
                    stream.Write(bArr, 0, size);
                    stream.Flush();
                    size = responseStream.Read(bArr, 0, (int)bArr.Length);
                }
                stream.Close();
                responseStream.Close();
                return strPath;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

    }
}

这个需要.net 4.5

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 干货 | 携程机票Node.js开发实践

    付文平,携程机票研发部前端开发总监。2011年加入携程,主要负责携程机票PC、H5、Hybrid业务方面的开发工作。先后负责机票PC前后端分离,H5 Swift...

    五月君
  • Flutter | WReorderList 一个可以指定两个item互换位置的组件

    最近遇到个需求,要求在一个 ListView 里面能互换两个 item 的位置,这样:

    Flutter笔记
  • 从零开始实现简单的webapi框架【Golang 入门系列十一】

    之前,已经讲过很多Golang的东西,比如基础语法,mysql的使用,redis的使用等等,感兴趣的可以看看以前的文章,https://www.cnblogs....

    章为忠
  • Vue项目中自动将px转换为rem

    Leophen
  • dotNET Core WebAPI 统一处理(返回值、参数验证、异常)

    现在 Web 开发比较流行前后端分离,我们的产品也是一样,前端使用Vue,后端使用 dotNet Core WebAPI ,在写 API 的过程中有很多地方需要...

    oec2003
  • How to run the CRM Fiori 1.0 applications in WebIDE

    I ran into some problem running the CRM My Opportunities application in WebIDE e...

    Jerry Wang
  • 什么是四层应用架构?

      随着移动应用的不断普及,传统后端三层架构已经不能再满足需要了,这种三层架构我们通常称为monolithic(巨石 整体的 铁板一块的)的架构。

    物流IT圈
  • 让 Spring Boot 启动更快一点

    这是 2018 Spring One Platform 中的一场会议。看完会议视频,我自己动手试了一下。下面是我做出的测试结果。

    芋道源码
  • ZABBIX4.2+Grafana6.1部署

     版权声明: https://blog.csdn.net/Callousmaster/article/details/89556528

    拓荒者
  • WEB前端-搜索引擎工作原理与SEO优化

    搜索引擎具有网络爬虫或蜘蛛来执行爬网,每次抓取工具访问网页时,它都会复制该网页并将其网址添加到索引中。

    Leophen

扫码关注云+社区

领取腾讯云代金券