0

I have the following code:

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[i].ToString();
    string[] proxyArray = proxy.Split(':');
    WebProxy proxyz = new WebProxy(proxyArray[0], int.Parse(proxyArray[1]));
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string str = reader.ReadToEnd();
    Thread.Sleep(100);
    {
      repeat++;
      continue;
    }
  }
  catch (Exception ex) //Incase some exception happens
  {
    listBox2.Items.Add("Error:" + ex.Message);
  }

I don't understand what I do wrong?

6
  • 3
    It would help if you told us what it IS or ISN'T doing Commented Jan 8, 2010 at 23:01
  • Basically, the feature of the program is; the user can load a proxy list into a list box, it will then browse a link specified in a text box; move onto the next proxy browse the page then move on etc.. It isn't browsing the page.. Commented Jan 8, 2010 at 23:10
  • Lawrence, I tried to reformat your code (basically trying to get the indenting consistent to make it more readable), but from what I could tell there was something wrong with it -- the try and catch blocks don't seem to match up. Could you double-check whether you have all the braces etc. that are in your actual program? Commented Jan 8, 2010 at 23:17
  • Yeah, all the braces match up.. I'm not sure what's wrong :( Commented Jan 8, 2010 at 23:23
  • OBTW, there's no such thing as a C# proxy. You meant a .NET proxy. Commented Jan 8, 2010 at 23:29

2 Answers 2

1

You're not setting Proxy on your HttpWebRequest. (You're creating a WebProxy object, but not using it.) You need to add:

request.Proxy = proxyz;

before calling request.GetResponse().

Sign up to request clarification or add additional context in comments.

Comments

1

You also need to fix your use of objects which implement IDisposable. Since they're created in a loop, you cannot delay this - it could be causing any amount of random damage:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    string[] proxyArray = proxyHostAndPort.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();
        }
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.