1. <rp id="c4hsd"></rp>
          1. <button id="c4hsd"><acronym id="c4hsd"><input id="c4hsd"></input></acronym></button>
          2. <tbody id="c4hsd"></tbody>

            <dd id="c4hsd"><pre id="c4hsd"></pre></dd>

              1. <th id="c4hsd"></th>
              2. <s id="c4hsd"><object id="c4hsd"></object></s>

                    [ 登錄 ] - [ 注冊 ] | 代碼示例DEMO | IP測試視頻 |

                    如何在C#中使用動態代理IP接口抓取數據

                    作者:數據無憂   時間:2020-09-18 13:24:53

                    下面為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 + "次請求測試");
                    				// 獲取動態IP				
                    				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{
                    					// 定義client
                    					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(www.aooseo.com)原創文章,轉載請注明出處。

                    電話:4007-745-096
                    QQ:
                    周一至周日8:30-18:00 技術部電話熱線
                    久久夜色精品国产噜噜亚洲AV_老妇女性较大毛片_888亚洲欧美国产va在线播放_超碰人人透人人爽人人看