简体   繁体   English

如何在 C# 应用程序中使用代理

[英]How to use proxy with C# application

I am using Microsoft Visual Studio 2010 C# .net 4.0我正在使用 Microsoft Visual Studio 2010 C# .net 4.0

I have a webbrowser element.我有一个网络浏览器元素。 What i want to do is navigating via Webbrowser element with using proxy.我想要做的是使用代理通过 Webbrowser 元素导航。 How can i do that?我怎样才能做到这一点? thank you.谢谢你。

The browser control is just an instance of IE - it will use IE's proxy settings.浏览器控件只是 IE 的一个实例——它将使用 IE 的代理设置。 You can set these by playing with registry keys if you must do it in code.如果您必须在代码中执行,您可以通过使用注册表项来设置这些。

        string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
        string serverName = "";//your proxy server name;
        string port = ""; //your proxy port;
        string proxy = serverName + ":" + port;

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

See this: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/da510380-9571-4fcd-a05f-b165ced45017/看到这个: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/da510380-9571-4fcd-a05f-b165ced45017/

Update : Looks like this can be done for just the control and not the entire machine.更新:看起来这可以只用于控制而不是整台机器。 See this code sample for setting the proxy for just a single process - 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

See this link.请参阅此链接。 You can easily set proxies for web requests but the WebBrowser class shares settings with iexplore.exe... If you want, you can adjust the proxy settings by programmatically changing the IE registry values and then change them back (see brendan's answer).您可以轻松地为 web 请求设置代理,但 WebBrowser class 与 iexplore.exe 共享设置...如果需要,您可以通过以编程方式更改 IE 注册表值来调整代理设置,然后将其更改回来(请参阅布伦丹的回答)。

How to set a proxy for Webbrowser Control without effecting the SYSTEM/IE proxy 如何在不影响 SYSTEM/IE 代理的情况下为 Webbrowser Control 设置代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM