手動測試代理 IP 繁瑣低效,利用 `HttpWebRequest` 自動化驗證,順便可延伸為採集器核心邏輯。
public void VerifyProxy(string ip, int port) {
WebProxy proxy = new WebProxy(ip, port);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.whatismyip.com.tw/");
req.Proxy = proxy;
try {
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8)) {
string html = sr.ReadToEnd();
// 解析頁面提取 IP 顯示區域
int start = html.IndexOf("<h2>") + 4;
int end = html.IndexOf("</h2>", start);
string detectedIp = html.Substring(start, end - start);
MessageBox.Show($"驗證成功!顯示 IP 為 {detectedIp}");
}
} catch {
MessageBox.Show("驗證失敗或讀取異常");
}
}