欢迎光临
我的个人博客网站

.NET C# 获取免费代理IP

66ip代理:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Security.Policy;  namespace Proxy {     public partial class Form1 : Form     {         private static string StrUrl = "http://www.66ip.cn/mo.php?sxb=&tqsl=10&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=";//实例化timer         System.Timers.Timer t = new System.Timers.Timer();         //定义委托变量         public happy updateTxt_ok;         public delegate void happy();          public Form1()         {             InitializeComponent();              //设定timer             t.Interval = 100000; //定时,单位ms             t.Enabled = true; //回调函数允许执行             t.AutoReset = false; //回调函数循环执行             t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;             t.Stop();         }          //timer事件         public void theout(object source, System.Timers.ElapsedEventArgs e)         {             this.BeginInvoke(updateTxt_ok);//委托执行updateTxt_ok         }          //委托执行函数         public void UpdateTxtMethod()         {             try             {                 HtmlDocument html = this.webBrowser1.Document;                 string IpStr = html.Body.InnerText.Trim().Replace("rn", ";");                 DBA.SqlDbAccess.ExecNoQuery(CommandType.Text, string.Format("insert into iptable(ip) values('{0}')", IpStr));                      }             catch (Exception ex)             {                 //记录日志                          }             webBrowser1.Navigate(StrUrl);             webBrowser1_DocumentCompleted(null, null);         }          private void button1_Click(object sender, EventArgs e)         {             webBrowser1.Navigate(StrUrl);             webBrowser1_DocumentCompleted(null, null);                 }                 private void Form1_Load(object sender, EventArgs e)         {             updateTxt_ok = new happy(UpdateTxtMethod);             button1_Click(null,null);         }          private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作         {             t.Start();//开启时钟         }     } }

89ip代理:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Security.Policy;  namespace 数据提取 {     public partial class Form1 : Form     {         private static string StrUrl_66 = "http://www.66ip.cn/nmtq.php?getnum=10&isp=0&anonymoustype=0&start=&ports=&export=&ipaddress=&area=0&proxytype=2&api=66ip";         private static string StrUrl_89 = "http://www.89ip.cn/tqdl.html?api=1&num=30&port=&address=&isp=";         private static string StrUrl = StrUrl_89;         //实例化timer         System.Timers.Timer t = new System.Timers.Timer();         //定义委托变量         public happy updateTxt_ok;         public delegate void happy();          public Form1()         {             InitializeComponent();              //设定timer             t.Interval = 100000; //定时,单位ms             t.Enabled = true; //回调函数允许执行             t.AutoReset = false; //回调函数循环执行             t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;             t.Stop();         }          //timer事件         public void theout(object source, System.Timers.ElapsedEventArgs e)         {             this.BeginInvoke(updateTxt_ok);//委托执行updateTxt_ok         }          //委托执行函数         public void UpdateTxtMethod()         {             try             {                 HtmlDocument html = this.webBrowser1.Document;                 string IpStr = html.Body.InnerText.Trim().Replace("更好用的代理ip请访问:https://www.7yip.cn/", "");                 if (IpStr.LastIndexOf("更好用的代理") > 0)                 {                     IpStr = IpStr.Substring(0, IpStr.LastIndexOf("更好用的代理")).Trim().Replace("rn", ";");                 }                 DBA.SqlDbAccess.ExecNoQuery(CommandType.Text, string.Format("insert into iptable(ip) values('{0}')", IpStr));                                }             catch (Exception ex)             {                 //记录日志                          }             webBrowser1.Navigate(StrUrl);             webBrowser1_DocumentCompleted(null, null);         }          private void button1_Click(object sender, EventArgs e)         {             webBrowser1.Navigate(StrUrl);             webBrowser1_DocumentCompleted(null, null);                 }                 private void Form1_Load(object sender, EventArgs e)         {             updateTxt_ok = new happy(UpdateTxtMethod);             button1_Click(null,null);         }          private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作         {             t.Start();//开启时钟         }     } }

以翻译接口演示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Text.RegularExpressions; using LibHW; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.IO; using System.Security.Cryptography;  namespace KXDataToDB {     public class TranslateApi     {         static List<string> lstIP = new List<string>();         static string ip = null;          //读取数据库         private void GetIPList()         {             string strip = DBA.SqlDbAccess.ExecuteScalar(System.Data.CommandType.Text, "select top 1 ip from iptable order by id desc").ToString();             foreach (string item in strip.Split(';'))             {                 string ip = "http://" + item.Trim();                 lstIP.Add(ip);             }         }private string getnewIP()         {             if (ip == null)             {                 if (lstIP.Count == 0)                 {                     GetIPList();                 }                 ip = lstIP[0].ToString();                 lstIP.RemoveAt(0);             }             return ip;         }public string TranslateGoogle(string q, string from, string to)         {             bool isError = true;             string result = string.Empty;             while (isError)             {                 try                 {                     string url = string.Format(                         "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids={0}&bl={1}&soc-app={2}&soc-platform={3}&soc-device={4}&rt={5}",                          "MkEWBc", "boq_translate-webserver_20210927.13_p0", "1", "1", "1", "c");                     var from_data = "f.req=" + System.Web.HttpUtility.UrlEncode(                         string.Format("[[["MkEWBc","[[\"{0}\",\"{1}\",\"{2}\",true],[null]]", null, "generic"]]]",                         ReplaceString(q), from, to), Encoding.UTF8).Replace("+", "%20");                     byte[] postData = Encoding.UTF8.GetBytes(from_data);                     WebClientPro client = new WebClientPro(10000);                     WebProxy proxy = new WebProxy();                     proxy.UseDefaultCredentials = false;                     proxy.Address = new Uri(getnewIP());                     client.Proxy = proxy;                     client.Encoding = Encoding.UTF8;                     client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");                     client.Headers.Add("ContentLength", postData.Length.ToString());                     client.Headers.Add("accept",                          "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");                     client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36");                     byte[] responseData = client.UploadData(url, "POST", postData);                     string content = Encoding.UTF8.GetString(responseData);                     result = MatchResult(content);                     System.Threading.Thread.Sleep(new Random().Next(200, 500));                     isError = false;                 }                 catch (Exception e)                 {                     ip = null;                     isError = true;                 }             }             return result;         }          /// <summary>         /// 匹配翻译结果         /// </summary>         /// <returns></returns>         public static string MatchResult(string content)         {             string result = "";             string patttern = @",[[\""(.*?)\"",";             Regex regex = new Regex(patttern);             MatchCollection matchcollection = regex.Matches(content);             if (matchcollection.Count > 0)             {                 List<string> list = new List<string>();                 foreach (Match match in matchcollection)                 {                     list.Add(match.Groups[1].Value);                 }                 result = string.Join(" ", list.Distinct());                 if (result.LastIndexOf(@"""]]]],""") > 0)                 {                     result = result.Substring(0, result.LastIndexOf(@"""]]]],"));                 }             }             return result;         }          /// <summary>         ///   替换部分字符串         /// </summary>         /// <param name="sPassed">需要替换的字符串</param>         /// <returns></returns>         public static string ReplaceString(string JsonString)         {             if (JsonString == null) { return JsonString; }             if (JsonString.Contains("\"))             {                 JsonString = JsonString.Replace("\", "\\");             }             if (JsonString.Contains("'"))             {                 JsonString = JsonString.Replace("'", "\'");             }             if (JsonString.Contains("""))             {                 JsonString = JsonString.Replace(""", "\\\"");             }             //去掉字符串的回车换行符             JsonString = Regex.Replace(JsonString, @"[nr]", "");             JsonString = JsonString.Trim();             return JsonString;         }
  }
}

WebClient重写:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net;  namespace KXDataToDB {     public class WebClientPro : WebClient     {         /// <summary>         /// 过期时间         /// </summary>         public int Timeout { get; set; }          public WebClientPro(int timeout = 30000)         {//默认30秒             Timeout = timeout;         }          /// <summary>         /// 重写GetWebRequest,添加WebRequest对象超时时间         /// </summary>         /// <param name="address"></param>         /// <returns></returns>         protected override WebRequest GetWebRequest(Uri address)         {//WebClient里上传下载的方法很多,但最终应该都是调用了这个方法             HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);             request.Timeout = Timeout;             request.ReadWriteTimeout = Timeout;             return request;         }     } }

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » .NET C# 获取免费代理IP
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏