代理云 代理ip测试

松正阳
2023-12-01
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace DataSyncTests
{
    [TestClass]
    public class UnitFlurl
    {
      
        [TestMethod]
        public void MyTestMethod()
        {
            string proxyIP;
            List<string> ips = new List<string>();
            for (int i = 0; i < 1000; i++)
            {
                if (ips.Count > 99)
                    break;
                proxyIP = Get("http://string.v4.dailiyun.com/query.txt?key=NPE3B31150&word=&count=45&rand=false&ltime=60&norepeat=false&detail=false", null).Trim();
                if (string.IsNullOrEmpty(proxyIP))
                    continue;
                ips.AddRange(proxyIP.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
            }

            string url = "https://docs.microsoft.com/_api/familyTrees/bymoniker/aspnetcore-5.0";
            //string url = "https://www.baidu.com";
            string host;
            int port;
            WebProxy proxy;

            string[] proxyip;
            List<string> jsons = new List<string>();
            string json;
            int successCount = 0;
            int errorCount = 0;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            foreach (var ip in ips)
            {
                proxyip = ip.Split(':');
                host = proxyip[0];
                port = int.Parse(proxyip[1]);
                proxy = new WebProxy(host, port);
                try
                {
                    json = Get(url, proxy);
                    successCount++;
                    Trace.WriteLine($"成功{successCount}次");
                    //Trace.WriteLine(json);
                    jsons.Add(json);
                }
                catch (Exception ex)
                {
                    errorCount++;
                    Trace.WriteLine($"错误{errorCount}次,错误信息:" + ex.Message);
                }
            }
            stopwatch.Stop();
            Trace.WriteLine($"{ips.Count}次请求,总耗时:{stopwatch.Elapsed},成功{successCount}次,失败{errorCount}次");
            Trace.WriteLine($"说明:请求页面 {url} 超时时间 3秒");
        }
        public static string Get(string url, WebProxy proxy)
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            request.Timeout = 1000 * 3;
            if (proxy != null)
            {
                proxy.Credentials = new NetworkCredential("账户", "密码");
                request.Proxy = proxy;
            }
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {
                    string json = streamReader.ReadToEnd();
                    return json;
                }
            }
        }
    }
}

 类似资料: