【问题标题】:Default Proxy IE默认代理 IE
【发布时间】:2013-12-26 09:46:35
【问题描述】:

如何在 C# .net 中设置 Internet Explorer 的默认代理?

我想让浏览器能够在 .txt 文件中搜索有效代理并使用它。
与 set default proxy 命令一起使用的 IP 和端口是有效的,但是该命令本身绝对没有任何作用。

我发现在 VS2013 .net 浏览器中使用代理的唯一方法是手动将其添加到 IE 中,这非常无用。

class Proxy
{
    List<string> Proxy_IP;
    List<int> Proxy_Port;

    public Proxy()
    {
        Proxy_IP = new List<string>();
        Proxy_Port = new List<int>();
        populateProxyList();
    }

    public void findProxy()
    {
        for (sbyte i = 0; i < Proxy_IP.Count; i++)
        {
            string IP = Proxy_IP[i];
            int Port = Proxy_Port[i];

            if (isValid(IP, Port))
            {
                setDefaultProxy(IP, Port);
                break;
            }
        }
    }

    private void populateProxyList()
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\Proxies.txt");
        foreach (string line in lines)
        {
            int endIP = line.IndexOf('#');
            int startPort = endIP + 1;
            int portLength = line.Length - (endIP + 2);

            string IP = line.Remove(endIP);
            int Port = Convert.ToInt32(line.Substring(startPort, portLength));

            Proxy_IP.Add(IP);
            Proxy_Port.Add(Port);
        }
    }

    private void setDefaultProxy(string ip, int port)
    {
        System.Windows.Forms.MessageBox.Show(ip + ":" + port.ToString()); // Proxy is valid
        System.Net.WebRequest.DefaultWebProxy = new WebProxy(ip, port); // Doesn't do shit
    }

    private bool isValid(string IP, int Port)
    {
        bool pingable = false;
        Ping pinger = new Ping();

        try
        {
            PingReply reply = pinger.Send(IP);
            pingable = reply.Status == IPStatus.Success;
        }
        catch (PingException)
        {

        }
        return pingable;
    }

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    正如 MSDN DefaultWebProxy 页面http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy%28v=vs.110%29.aspx 中所说,

    DefaultWebProxy 属性从 app.config 读取代理设置 文件。如果没有配置文件,则当前用户的 Internet Explorer 使用 (IE) 代理设置。

    因此,如果您的问题是设置代理仅用于在 Visual Studio 中浏览(2013 适合您),您应该在 Visual Studio 的配置文件中添加代理信息 ,命名为“devenv.exe.config”:

    <system.net>
        <defaultProxy useDefaultCredentials=“true“ enabled=“true“>
            <proxy bypassonlocal=”true” proxyaddress=”http://yourproxyaddress.net:proxyPort” />
        </defaultProxy>
    </system.net>
    

    更多详情请看这篇文章:http://en.code-bude.net/2013/07/15/how-to-setup-a-proxy-server-in-visual-studio-2012/

    如果您的问题是为所有浏览器设置代理(即:默认互联网代理),您应该使用 PInvoke 并更改注册表设置:请参阅此处的回复How to change Global Windows Proxy using C# .NET with `Immediate Effect`

    【讨论】:

    • 有没有办法以编程方式设置它?在执行程序之前手动设置它会花费太多时间。
    • 您需要经常更改您的代理参数(例如在客户站点使用代理,而不是在家中)?对于这种情况,我更喜欢使用“IEProxy”之类的软件在我的 Internet 选项中设置代理(而不是修改 VS 的配置文件),它可以让您快速切换代理。它必须使用与我后面的第二个解决方案相同的代码行
    • 您能再次发表您的上一条评论吗?我的收件箱中有开头,但在此页面中看不到它。你删了吗?
    【解决方案2】:
       WebProxy webProxy = (WebProxy) WebRequest.DefaultWebProxy;
       if (webProxy.Address.AbsoluteUri != string.Empty)
       {
           Console.WriteLine("Proxy URL: " + webProxy.Address.AbsoluteUri);
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多