0

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,

1 Answer 1

1

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.

2
  • I would be greatly helped if you give an example that can be applied in my code, i still confused with this one
    – userrrrrrr
    Jan 17, 2017 at 18:55
  • Remove the for loops of your code and put the rest of it inside the for loop I wrote, then instead of "prox" and "kw" use "proxy" and "keyword", that's all.
    – Gusman
    Jan 17, 2017 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.