使用代理与 webBrowser 控件 C#/.net 3.5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12042591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:12:01  来源:igfitidea点击:

Use a proxy with webBrowser control C#/.net 3.5

c#.netproxywebbrowser-controlip-address

提问by user1608298

I need some help from someone who has already use the webBrowser control along with a proxys.

我需要已经使用 webBrowser 控件和代理的人的帮助。

What I need is the following.

我需要的是以下内容。

1 - Set a proxy for a webBrowser control. 2 - Load a specific site. 3 - Execute a routine over the site. 4 - Set a diferent proxy for the webBrowser control. 5 - Load another site. 6 - Execute the same routine from point number 3.

1 - 为 webBrowser 控件设置代理。2 - 加载特定站点。3 - 在站点上执行例程。4 - 为 webBrowser 控件设置不同的代理。5 - 加载另一个站点。6 - 从第 3 点开始执行相同的程序。

And the process keeps in that way, looping from a list of proxys, until all of them had been used.

并且该过程保持这种方式,从代理列表循环,直到所有代理都被使用。

But. I'm having some problems with the app. to do that:

但。我在使用该应用程序时遇到了一些问题。要做到这一点:

1 - I'm using the code attached to set the proxy into the webBrowser control, but seems to work only once during the execution, when I call it again in the loop it just doesn't work, I can get t ounderstand why.

1 - 我正在使用附加的代码将代理设置到 webBrowser 控件中,但在执行期间似乎只工作一次,当我在循环中再次调用它时它不起作用,我无法理解为什么。

2 - I'm having problems to determine when the page has loaded completely, I mean, when I set the first site to load, I need the program to wait until it has finish to load, and after that execute the routine over it, and continue with the process.

2 - 我在确定页面何时完全加载时遇到问题,我的意思是,当我将第一个站点设置为加载时,我需要程序等待它完成加载,然后在它上面执行例程,并继续该过程。

Hope some one could help me with this...

希望有人能帮我解决这个问题...

/// The function that I'm using -----------------------------

/// 我正在使用的函数 -----------------------------

    private void SetProxy(string Proxy)
    {

        MessageBox.Show("Setting :" + Proxy);
        string key = "Software\Microsoft\Windows\CurrentVersion\Internet Settings";

        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(key, true);
        RegKey.SetValue("ProxyServer", Proxy);
        RegKey.SetValue("ProxyEnable", 1);

    }

// The app logic --------------------------------------

// 应用逻辑--------------------------------------

        SetProxy("190.97.219.38:80");
        webBrowser1.Navigate("http://www.whatismyip.com/");
        ExecuteRoutine();

        SetProxy("187.93.77.235:80");
        webBrowser1.Navigate("http://www.whatismyip.com/");
        ExecuteRoutine();

        SetProxy("109.235.49.243:80");
        webBrowser1.Navigate("http://www.whatismyip.com/");
        ExecuteRoutine();

回答by L.B

1- I guess webBrowser control checks the proxy only while its is created, so create a new control after setting the proxy

1- 我猜 webBrowser 控件仅在创建代理时检查代理,因此在设置代理后创建一个新控件

2- Navigate is not a blocking call and does not wait till page it loaded, use webBrowser.DocumentCompletedevent

2-导航不是阻塞调用,不会等到页面加载,使用webBrowser.DocumentCompleted事件

Below code should work (Not tested)

下面的代码应该可以工作(未测试)

void Exec(string proxy,string url)
{
    var th = new Thread(() =>
    {
        SetProxy(proxy);
        using (WebBrowser wb = new WebBrowser())
        {
            wb.DocumentCompleted += (sndr, e) =>
            {
                ExecuteRoutine();
                Application.ExitThread();
            };
            wb.Navigate(url);
            Application.Run();
        }
    });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
    th.Join();
}

回答by Uwe Keim

I had a somewhat similar questionin the past. The accepted answerfor the question suggests to take a look at this Microsoft Knowledge Base article:

我过去有一个有点类似的问题。该问题的公认答案建议查看此 Microsoft 知识库文章:

"How to programmatically query and set proxy settings under Internet Explorer"

如何在 Internet Explorer 下以编程方式查询和设置代理设置

Basically, you have to do some P/Invoke and call some WinInet DLL functions. Although I never tried it in a real-world project, I strongly assume that this is the way to go.

基本上,您必须执行一些 P/Invoke 并调用一些 WinInet DLL 函数。尽管我从未在实际项目中尝试过,但我强烈认为这是可行的方法。

回答by Erx_VB.NExT.Coder

Just to let you all know, this guy has posted 5 question, all asking the same thing, and based on his first question and how badly he was knocked down, it seems he is trying to commit some type of cybercrime. Now, based on my reading of his intellect, he'll probably end up in prison really quickly, but I'm just thinking perhaps we can save him from that by letting him know that it's not possible to provide an imaginary IP address to services you are communicating with (since if you did, the service will not be able to reach you to provide a response). Here is his entertaining list:

只是为了让大家知道,这个人已经发布了 5 个问题,都问了同样的问题,根据他的第一个问题以及他被击倒的严重程度,他似乎试图实施某种类型的网络犯罪。现在,根据我对他智力的解读,他可能很快就会入狱,但我只是在想,也许我们可以通过让他知道无法为服务提供虚构的 IP 地址来避免他陷入困境您正在与之通信(因为如果您这样做了,该服务将无法联系到您以提供响应)。这是他的娱乐清单:

https://stackoverflow.com/questions/12045317/how-to-hide-my-ip-address-c-net-3-5

https://stackoverflow.com/questions/12045317/how-to-hide-my-ip-address-c-net-3-5

Use a proxy with webBrowser control C#/.net 3.5

使用代理与 webBrowser 控件 C#/.net 3.5

how to pass ip-address to webBrowser control

如何将 ip 地址传递给 webBrowser 控件

how to use custom ip address to browse a web page c#/.net

如何使用自定义IP地址浏览网页c#/.net

https://stackoverflow.com/questions/12019890/how-to-load-webpage-using-user-provided-ipaddress-webbrowser-control-c-net

https://stackoverflow.com/questions/12019890/how-to-load-webpage-using-user-provided-ipaddress-webbrowser-control-c-net

And now, I think he has created a new username, user1563019, with more proxy/settings questions below: https://stackoverflow.com/users/1563019/user1563019

现在,我认为他创建了一个新用户名 user1563019,下面有更多代理/设置问题:https://stackoverflow.com/users/1563019/user1563019

回答by P. R. 117

Perhaps this link is useful:

也许这个链接很有用:

http://blogs.msdn.com/b/jpsanders/archive/2011/04/26/how-to-set-the-proxy-for-the-webbrowser-control-in-net.aspx

http://blogs.msdn.com/b/jpsanders/archive/2011/04/26/how-to-set-the-proxy-for-the-webbrowser-control-in-net.aspx

I tested the code and it seemed to work. But two points are important:

我测试了代码,它似乎有效。但有两点很重要:

  • It's not compatible to projects in compile mode "Any CPU" (x86 works fine)
  • JUST for HTTP proxy servers ; not for SOCKS
  • 它与编译模式“任何 CPU”下的项目不兼容(x86 工作正常)
  • 仅适用于 HTTP 代理服务器;不适合袜子