简体   繁体   English

在C#中设置代理?

[英]Set a proxy in C#?

I want to set a proxy in C#.NET, the thing is, how would I make it so that all web requests and web browser controls connect through the proxy? 我想在C#.NET中设置一个代理,问题是,我将如何使所有Web请求和Web浏览器控件通过代理连接? Like a SOCKS proxy? 像SOCKS代理一样?

I need it to apply to any web browser controls or any web requests. 我需要将其应用于任何Web浏览器控件或任何Web请求。 It would be best if I could just set it so all outgoing requests from the user's machine is sent through the proxy. 最好是将其设置为使来自用户计算机的所有传出请求都通过代理发送。

By default, your C# code will use the system proxy set in your IE connection settings. 默认情况下,您的C#代码将使用IE连接设置中设置的系统代理。

If you want to be explicit, you can do it via config under System.net: 如果要明确,可以通过System.net下的config来实现:

<defaultProxy useDefaultCredentials="true">
    <proxy usesystemdefault="true" proxyaddress="[proxy address]" bypassonlocal="true" />
</defaultProxy>

Programmatically, you can set the proxy: 您可以通过编程方式设置代理:

http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx http://msdn.microsoft.com/zh-CN/library/system.net.webproxy.aspx

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;

EDIT: For web browser control, try this SO post: (disclaimer - haven't tried that) 编辑:对于Web浏览器控件,请尝试以下SO帖子:(免责声明-尚未尝试过)

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