Почему HttpWebrequest через .Net прокси не работает? - PullRequest
0 голосов
/ 09 января 2010

У меня есть следующий код:

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);
  }

Я не понимаю, что я делаю не так?

Ответы [ 2 ]

1 голос
/ 09 января 2010

Вам также необходимо исправить использование объектов, которые реализуют IDisposable. Так как они созданы в цикле, вы не можете отложить это - это может привести к любому случайному ущербу:

    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();
        }
    }
1 голос
/ 09 января 2010

Вы не устанавливаете прокси в своем запросе HttpWebRequest. (Вы создаете объект WebProxy, но не используете его.) Вам необходимо добавить:

request.Proxy = proxyz;

перед вызовом request.GetResponse ().

...