Need Proxy?

BotProxy: Rotating Proxies Made for professionals. Really fast connection. Built-in IP rotation. Fresh IPs every day.

Find out more


Working with proxy list c#

Question

I have 2 listbox,

  • lbProxy = contain proxy list
  • lbKeyword = contain keyword list.

I want to process the keyword list, 1 keyword using 1 proxy. If number of proxy list less than keyword list, then loop proxy list again.

I don't know how to change proxy after 1 keyword is processed.

Full Code

private void useproxy()
{
       foreach (string prox in lbProxy.Items)
       {
            WebClient fetch = new WebClient();
            string numResults = nudPages.Value.ToString();
            int delay = Convert.ToInt32(nudDelay.Value);
            if (lblStatusProxy.InvokeRequired)
            {
                lblStatusProxy.Invoke(new MethodInvoker(delegate
                {
                    lblStatusProxy.Text = "Using: " + prox;
                }));
            }
            WebProxy wp = new WebProxy(prox);
            fetch.Proxy = wp;

            foreach (string kw in lbKeyword.Items)
            {
                keywords = kw;
                if (lblStatusKeyword.InvokeRequired)
                {
                    lblStatusKeyword.Invoke(new MethodInvoker(delegate
                    {
                        lblStatusKeyword.Text = "Processing Keyword :" + kw;
                    }));
                }
                string downloadUrl = "https://www.google.com" + "/search?q=" + kw + "&num=" + numResults + "&as_qdr=all&ei=LrUVVf7UMrPfsAS7lICgCw&sa=N&biw=1440&bih=690";
                fetch.Headers.Set(HttpRequestHeader.Host, "www.google.com");
                string data = fetch.DownloadString(downloadUrl);
                string[] results = TopUrls(data);

                foreach (string lines in results)
                {
                    if (lbBlog.InvokeRequired)
                    {
                        lbBlog.Invoke(new MethodInvoker(delegate
                        {
                            lbBlog.Items.Add(lines);
                        }
                        ));
                    }
                }
            }
       }
}

Thank you,

Answer

You can use a plain for and the modulus operand:

for(int buc = 0; buc < lbKeyword.Items.Count; buc++)
{
    var proxy = lbProxy.Items[buc % lbProxy.Items.Count];
    var keyword = lbKeyword.Items[buc];

    //Now you have keyword and proxy to continue with your code.
}

The trick here is the modulus operand, as it's aplied over lbProxy.Items.Count it will give you a value between 0 and lbProxy.Items.Count - 1, just the index you were looking for.

cc by-sa 3.0