如何在c#語(yǔ)言代碼中使用HTTP代理IP。
以下代碼主要圍繞第一次接觸HTTP代理IP的c#新手來(lái)寫(xiě)(步驟注釋清晰)。
直接把下面示例代碼中的HTTP代理API,替換成你后臺(tái)生成的代理API鏈接,就可以跑起來(lái)了。
以下是一個(gè)示例代碼,只是一個(gè)基礎(chǔ)的演示,具體的代碼還是要根據(jù)你業(yè)務(wù)的實(shí)際情況去寫(xiě)的。
示例代碼中的HTTP代理IP,我使用的是華益云的HTTP代理API,注冊(cè)就白嫖1萬(wàn)個(gè)高匿爬蟲(chóng)IP,有效期是一年,對(duì)于調(diào)試代碼來(lái)說(shuō)這個(gè)時(shí)間是非常的友好。
華益云-企業(yè)級(jí)HTTP爬蟲(chóng)代理IP供應(yīng)商-點(diǎn)我免費(fèi)領(lǐng)取示例代碼demo中同款10000個(gè)高匿IP
打開(kāi)代理API,獲取里面的IP,使用IP訪問(wèn)目標(biāo)網(wǎng)站,其實(shí)代碼中就是執(zhí)行這個(gè)過(guò)程而已,然后加了幾個(gè)錯(cuò)誤判斷有助于代碼的穩(wěn)定運(yùn)行。(步驟注釋清晰)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; namespace proxyRequests { class ProxyInfo { private string host; private int port; public ProxyInfo(string host, int port) { this.host = host; this.port = port; } public string getHost() { return host; } public int getPort() { return port; } } class Program { static void Main(string[] args) { // 發(fā)送給服務(wù)器的標(biāo)識(shí) string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/532.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"; // 代理api(這里我推薦使用www.9vps.com華益云的HTTP代理API,注冊(cè)就白嫖1萬(wàn)IP) string proxyUrl = "http://http.9vps.com/getip.asp?username=166xxxx6597&pwd=xxxxbaa59ce237dff65134984b9cxxxx&geshi=1&fenge=1&fengefu=&Contenttype=1&getnum=20&setcity=&operate=all&"; List outPutProxy = getProxy(proxyUrl, userAgent); if (outPutProxy.Count == 0) { return; } // 目標(biāo)請(qǐng)求網(wǎng)站 string url = "https://www.qq.com/"; outPutProxy.Add(new ProxyInfo("1", 0)); for (int i = 0; i < 3; i++) { // 最多嘗試三次 try { ProxyInfo px = outPutProxy[0]; outPutProxy.Remove(px); WebProxy proxy = new WebProxy(px.getHost(), px.getPort()); string outHtml = requestGet(url, userAgent, proxy); Console.WriteLine(outHtml); break; } catch (Exception e) { Console.WriteLine(e.StackTrace); if (outPutProxy.Count == 0) { // 如果發(fā)現(xiàn)沒(méi)有代理了,就去獲取下。 outPutProxy = getProxy(proxyUrl, userAgent); } } } //Console.WriteLine(requestGet(@"https://www.baidu.com/", userAgent)); //Console.ReadKey(); } static List getProxy(string proxyUrl, string userAgent) { //String proxyUrl = "http://http1.9vps.com/getip.asp?username=用戶(hù)名&pwd=API密碼串&geshi=1&fenge=1&fengefu=&Contenttype=1&getnum=2"; string proxyIps; List outPutProxy = new List(); try { proxyIps = requestGet(proxyUrl, userAgent, null); Console.WriteLine(proxyIps); // {"code":3002,"data":[],"msg":"error!用戶(hù)名或密碼錯(cuò)誤","success":false} if (proxyIps.Contains("{")) { throw new Exception("[錯(cuò)誤]" + proxyIps); } String[] splitedString = proxyIps.Split('\n'); for (int i = 0; i < splitedString.Length; i++) { /* * 180.104.192.217:22036 * 150.104.192.217:21036 */ String[] ret = splitedString[i].Split(':');// 180.104.192.217:22036 String host = ret[0]; // 180.104.192.217 int port = int.Parse(ret[1]); // 22036 outPutProxy.Add(new ProxyInfo(host, port)); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } Console.WriteLine("總共獲取了" + outPutProxy.Count + "個(gè)代理"); return outPutProxy; } static string requestGet(string url, string userAgent, WebProxy proxy) { WebClient client = new WebClient(); if (proxy != null) { // 設(shè)置代理部分 client.Proxy = proxy; } // 設(shè)置編碼解析方式為 UTF-8,防止中文亂碼 client.Encoding = Encoding.UTF8; client.Headers.Add("user-agent", userAgent); return client.DownloadString(url); } } }
由于素材太多,遇到文件缺失跑不起來(lái)的情況,可到網(wǎng)站下載完整版華益云幫助文檔-使用HTTP代理示例代碼
或者直接百度搜索:華益云HTTP代理
審核編輯:湯梓紅
-
API
+關(guān)注
關(guān)注
2文章
1613瀏覽量
64018 -
HTTP
+關(guān)注
關(guān)注
0文章
525瀏覽量
33497 -
代碼
+關(guān)注
關(guān)注
30文章
4900瀏覽量
70718
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論