C#在webBrowser1.Navigate中使用代理导致我的for循环崩溃

时间:2017-02-03 22:40:07

标签: c#

我一直在尝试使用以下stackoverflow答案在我的webBrowser1.Navigate请求上使用代理:https://stackoverflow.com/a/9036593/7443548

但是这段代码会导致我的for循环崩溃,我在自己的代码中运行。

我如何在代码函数中为每个请求使用代理?

需要使用代理的功能:

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Uri currentUri = new Uri(bunifuMaterialTextbox1.Text);

    int views = bunifuSlider1.Value;

    for (int i = 0; i < views; i++)
    {
        webBrowser1.Navigate(currentUri);
        System.Threading.Thread.Sleep(3000);
        backgroundWorker.ReportProgress(i + 1);
    }
}

我的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows.Forms;

namespace YoutubeViewBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
            backgroundWorker.WorkerReportsProgress = true;
        }

        youtubeViewBot youtubeBot = new youtubeViewBot();
        List<string> proxyList = new List<string>();

        private void bunifuImageButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void bunifuSlider1_ValueChanged(object sender, EventArgs e)
        {
            bunifuCustomLabel5.Text = bunifuSlider1.Value + " Views will be added.";
        }

        private void bunifuFlatButton3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                proxyList = youtubeBot.getProxyList(openFileDialog1.FileName);
                bunifuCustomLabel2.Text = proxyList.Count().ToString();
            }
        }

        private void bunifuFlatButton2_Click(object sender, EventArgs e)
        {
            if (!backgroundWorker.IsBusy)
            {
                backgroundWorker.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("There is already a process running.", "YoutubeViewbot response");
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Uri currentUri = new Uri(bunifuMaterialTextbox1.Text);

            int views = bunifuSlider1.Value;

            for (int i = 0; i < views; i++)
            {
                webBrowser1.Navigate(currentUri);
                System.Threading.Thread.Sleep(3000);
                backgroundWorker.ReportProgress(i + 1);
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            bunifuCustomLabel7.Text = e.ProgressPercentage.ToString();
            bunifuCustomLabel7.Update();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bunifuCustomLabel9.Text = "Finished";
            MessageBox.Show("Finished", "YoutubeViewBot response");
        }

        private void bunifuFlatButton1_Click(object sender, EventArgs e)
        {

        }
    }
}

这里是我尝试使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows.Forms;

namespace YoutubeViewBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
            backgroundWorker.WorkerReportsProgress = true;
        }

        youtubeViewBot youtubeBot = new youtubeViewBot();
        List<string> proxyList = new List<string>();
        Uri currentUri;

        private void bunifuImageButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void bunifuSlider1_ValueChanged(object sender, EventArgs e)
        {
            bunifuCustomLabel5.Text = bunifuSlider1.Value + " Views will be added.";
        }

        private void bunifuFlatButton3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                proxyList = youtubeBot.getProxyList(openFileDialog1.FileName);
                bunifuCustomLabel2.Text = proxyList.Count().ToString();
            }
        }

        private void bunifuFlatButton2_Click(object sender, EventArgs e)
        {
            if (!backgroundWorker.IsBusy)
            {
                backgroundWorker.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("There is already a process running.", "YoutubeViewbot response");
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Uri currentUri = new Uri(bunifuMaterialTextbox1.Text);

            int views = bunifuSlider1.Value;

            for (int i = 0; i < views; i++)
            {
                Random rnd = new Random();
                int r = rnd.Next(proxyList.Count);
                string proxy = proxyList[r];

                currentUri = new Uri(bunifuMaterialTextbox1.Text);
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri);
                WebProxy myProxy = new WebProxy(proxy);
                myRequest.Proxy = myProxy;
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                webBrowser1.DocumentStream = myResponse.GetResponseStream();
                webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

                backgroundWorker.ReportProgress(i + 1);
            }
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.AbsolutePath != "blank")
            {
                currentUri = new Uri(currentUri, e.Url.AbsolutePath);
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri);

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                webBrowser1.DocumentStream = myResponse.GetResponseStream();
                e.Cancel = true;
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            bunifuCustomLabel7.Text = e.ProgressPercentage.ToString();
            bunifuCustomLabel7.Update();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bunifuCustomLabel9.Text = "Finished";
            MessageBox.Show("Finished", "YoutubeViewBot response");
        }

        private void bunifuFlatButton1_Click(object sender, EventArgs e)
        {

        }
    }
}

应该采取的步骤:

  1. 点击按钮:bunifuFlatButton2_Click它应该启动backgroundWorker_DoWork功能
  2. backgroundWorker_DoWork应该循环运行x次,每次运行时都应该为请求附加一个新代理
  3. 我会感激任何帮助。

    修改1:

    在逐步调试时,我意识到for循环在第二次在以下行上运行后崩溃:

    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    

    视频可以在这里看到:

    https://www.youtube.com/watch?v=H3y2jFvuK3M

    在运行for循环两次而不是454次后完成。

0 个答案:

没有答案