下面为C#中调用动态代理IP抓取数据的代码,需要修改代码中的order属性为正确的API订单号
using System;
namespace TestDynamicIp{
class Program{
static string order = "这里需要改成无忧代理IP的接口单号,示例才可以运行";
static bool run = true;
static string ipApi = "http://api.ip.data5u.com/dynamic/get.html?order=" + order;
static string targetUrl = "http://1212.ip138.com/ic.asp";
public static void Main(string[] args){
Console.WriteLine("\n********************\nC#测试动态代理IP\n********************\n");
CrawlIP138();
Console.WriteLine("\n********************\n测试结束,按任意键退出程序\n********************\n");
Console.ReadKey(true);
}
public static void CrawlIP138(){
for(int i = 0; i< 10; i++){
Console.WriteLine("\n>>>>>>>>>>>>>>>>>>>>>>>第" + i + "次请求测试");
Uri uri = new Uri(ipApi);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(ipApi);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
String iptxt = reader.ReadToEnd();
reader.Dispose();
reader.Close();
string[] ipports = iptxt.Split(new String[]{"\n"}, StringSplitOptions.None);
System.Threading.Thread.Sleep(1000);
try{
string ipport = ipports[(int)(ipports.Length * new Random().Next(0, 1))];
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.GetEncoding("GB2312");
System.Net.WebProxy proxy = new System.Net.WebProxy();
proxy.Address = new Uri("http://" + ipports[0] + "/");
client.Proxy = proxy;
byte[] byResponse = client.DownloadData(targetUrl);
String txt = System.Text.Encoding.GetEncoding("GB2312").GetString(byResponse);
Console.WriteLine("使用代理" + ipports[0] + "得到如下内容:\n" + txt);
client.Dispose();
}catch(Exception e){}
}
}
}
}
福利:现在通过开发者购买代理IP,购买后联系客服最高可获
返现20%
到你的支付宝(最低35元,最高1440元)
无忧代理IP(www.data5u.com)原创文章,转载请注明出处。