views:

167

answers:

1

Okay I need help, again! For some reason it is not working, no idea why.. nothing even appears on my catch request..

public void load(object sender, DoWorkEventArgs e)
    {
        int repeat = 1;
        int proxyIndex = 1;
        if (listBox1.Items.Count == proxyIndex) //If we're at the end of the proxy list
        {
            proxyIndex = 0; //Make the selected item the first item in the list
        }
        try
        {
            int i = 0;
            while (i < listBox1.Items.Count)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(textBox1.Text);
                string proxy = listBox1.Items[1].ToString();
                string[] proxyArray = proxy.Split(':');
                WebProxy proxyz = new WebProxy(proxyArray[0], int.Parse(proxyArray[1]));
                request.Proxy = proxyz;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string str = reader.ReadToEnd();
                    }
                }

                    /*HttpWebRequest request = (HttpWebRequest)WebRequest.Create(textBox1.Text);
                    string proxy = listBox1.Items[i].ToString();
                    string[] proxyArray = proxy.Split(':');
                    WebProxy proxyz = new WebProxy(proxyArray[0], int.Parse(proxyArray[1]));
                    request.Proxy = proxyz;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string str = reader.ReadToEnd();
                    Thread.Sleep(100);
                    {
                        if (str != null)
                        {
                            listBox2.Items.Add("Starting connection.");
                            Thread.Sleep(1000);
                            {
                                listBox2.Items.Add("Waiting..");
                                Thread.Sleep(500);
                                {
                                    listBox2.Items.Add("Connection closed.");
                                    repeat++;
                                    continue;
                                }
                            }
                        }
                        else if (str == null)
                        {
                            listBox2.Items.Add("Reply was null, moving on.");
                            proxyIndex++;
                            repeat++;
                        }
                    }
                     */
                }
            }
        catch (Exception ex) //Incase some exception happens
        {
            MessageBox.Show(ex.Message);
            return;
            // listBox2.Items.Add("Error:" + ex.Message);
        }
    }

How can I get it to work?

A: 

It looks like you're trying to use a BackgroundWorker to perform this operation, and in the absence of any more detailed information on what isn't working, I'd guess it's because you aren't assigning any result or errors which can be picked up by main thread.

You should assign the results of the request in the case of success:

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    e.Result = reader.ReadToEnd();
}

Since you seem to be making multiple requests you should probably make the result a List<string> or similar.

You should remove the try/catch block and deal with any errors in the RunWorkerCompleted event of the BackgroundWorker:

private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.Error != null)
    {
        MessageBox.Show("Error in async operation: " + ex.Message);
    }
    else
    {
        //process results
    }
}
Lee
Thanks Lee, I still cannot get my head around it.. I don't like proxies too much, is there anyway in which I could send you the whole C# project for you to take a look at?
Lawrence