【问题标题】:Programmatically Set Proxy Address, Port, Username, Password以编程方式设置代理地址、端口、用户名、密码
【发布时间】:2011-05-15 00:08:06
【问题描述】:

您好,我需要以编程方式设置 IE 的代理地址

之前我用过这个RedTomahawk.TORActivator 但它没有提供设置需要用户名和密码的代理的选项。

我们如何设置那些需要用户名和密码的代理

请提供类似的例子

void Setproxy(string ip,string port,string uname,string pwd) { ///Code here }

【问题讨论】:

    标签: c# .net proxy


    【解决方案1】:

    您可以 P/Invoke WinHttpSetDefaultProxyConfiguration 函数。


    更新:

    包括要求的示例:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WINHTTP_PROXY_INFO
    {
        public AccessType AccessType;
        public string Proxy;
        public string Bypass;
    }
    
    public enum AccessType
    {
        DefaultProxy = 0,
        NamedProxy = 3,
        NoProxy = 1
    }
    
    class Program
    {
        [DllImport("winhttp.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern bool WinHttpSetDefaultProxyConfiguration(ref WINHTTP_PROXY_INFO config);
    
        static void Main()
        {
            var config = new WINHTTP_PROXY_INFO();
            config.AccessType = AccessType.NamedProxy;
            config.Proxy = "http://proxy.yourcompany.com:8080";
            config.Bypass = "intranet.com";
    
            var result = WinHttpSetDefaultProxyConfiguration(ref config);
            if (!result)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            else
            {
                Console.WriteLine("Successfully modified proxy settings");
            }
        }
    }
    

    【讨论】:

    • 请提供一个例子:- void setproxy(string proxy,string port,string username,string password){ ///code here }
    • - 您好,先生,我实际上是在使用 WatIN IE 来处理一些请求。发生的事情是:经过几次请求后,我的 ip 被阻止了。那时我想更改我的 IP 地址(我有一些有效的代理地址,格式为 xxx.xxx.xxx.xxx:xxxx:username:password)。使用 RedTomahawk.TORActivator 我可以轻松设置 ip:port 但现在我的代理列表也要求输入用户名密码。我的问题是如何以编程方式设置这些用户名、密码。请帮忙
    • 您的代理使用什么类型的身份验证?如果是基本身份验证,您可以尝试这样设置:http://username:password@yourproxy.com:8080。如果它是集成的 Windows 身份验证,它将取决于运行您的程序的用户。
    • 非常感谢 Darin Dimitrov 爵士对我的帮助。我知道可能没有直接设置用户名密码的直接方法。请查看此链接以了解我的实际问题stackoverflow.com/questions/4276044/… 并请帮助
    • @DarinDimitrov 代码不起作用。它返回“true”,但代理设置没有任何改变。你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多