Search code examples
c#foreachproxywebrequest

Using new proxy each loop


I am trying to make my index checker work through proxies since we're about to add a anti-dos filter to it. I've kind of managed to get it to work, but as you can see by the code, it uses one line (email&index) and uses all proxies on it, then moves on to the next line and uses all proxies on that line, and so on. (Because of the foreach loop inside the foreach loop)

But I want it to use one line + one proxyline for each line, although I'm not sure how to go about... (In other words what I want is for it to not loop and use all the proxylines in the foreach (string proxyline in proxylines) before it goes on the the next line (foreach (string line in lines)).

Now: email1&index1 proxy1 email1&index1 proxy2 email1&index1 proxy3

What I am trying to accomplish: email1&index1 proxy1 email2&index2 proxy2 email3&index3 proxy3

Sorry if I am being unclear, but if so, please let me know and I'll do my best to explain it better.

System.IO.StreamReader file = new System.IO.StreamReader(@"input.txt");

List<string> lines = new List<string>(File.ReadAllLines(@"C:\input.txt"));
List<string> proxylines = new List<string>(File.ReadAllLines(@"C:\proxies.txt"));

foreach (string line in lines) //here i am reading my emails & indexes
{
    string[] data = line.Split(':');
    string email = data[0];
    string index = data[1];

    using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
    {


        foreach (string proxyline in proxylines) //here i am reading my proxies line by line
        {
            WebRequest request = WebRequest.Create("https://website.com");
            string[] proxydata = proxyline.Split(':');
            string proxy = proxydata[0];
            int port = Int32.Parse(proxydata[1]);
            request.Proxy = new WebProxy(proxy, port);
            Console.WriteLine(proxy + ":" + port);
            request.Method = "POST";
            string postData = ("{\"email\":\"" + email + "\",\"index\":\"" + index+ "\"}");
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/json";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();



            dynamic json = JObject.Parse(responseFromServer);

            if (json["index"] != null)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(email + ":" + index);
                Console.ResetColor();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(email + ":" + index);
                Console.ResetColor();
            }


            reader.Close();
            dataStream.Close();
            response.Close();

        }
    }
}
Console.ReadKey();

So, would you know any ways to go about for this?


Solution

  • You can go through both lists in the same time using a for loop instead of foreach:

            string[] lines = File.ReadAllLines(@"C:\input.txt");
            string[] proxylines = File.ReadAllLines(@"C:\proxies.txt");
    
            for (int i = 0; i < Math.Min(lines.Length, proxylines.Length); i++)
            {
                string line = lines[i];
                string proxy = proxylines[i];
    
                // actions ...
            }
    

    -I replaced List<> by array declaration to improve performance (due to for loop, we access lists items through index numbers)

    -Math.Min is used as a security, since both list can have different number of lines