none
请问怎么设置webbrowser设置代理用户名和密码? RRS feed

  • 问题

  •         public struct Struct_INTERNET_PROXY_INFO
            {
                public int dwAccessType;
                public IntPtr proxy;
                public IntPtr proxyBypass;
            };
            [DllImport("wininet.dll", SetLastError = true)]
            private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
            //设置代理用户名和密码
            public void ProxyAuth()
            {
                const int INTERNET_OPTION_PROXY_USERNAME = 43;
                const int INTERNET_OPTION_PROXY_PASSWORD = 44;
                string username = "user";
                string password = "password";
                IntPtr intptrUsername = Marshal.StringToHGlobalAnsi(username);
                bool uReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, intptrUsername, System.Text.Encoding.ASCII.GetByteCount(username));
                IntPtr intptrPassword = Marshal.StringToHGlobalAnsi(password);
                bool pReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, intptrPassword, System.Text.Encoding.ASCII.GetByteCount(password));
            }

    使用不用认证的代理没问题,可是如果代理需要用户名和密码就不行了,一直弹出windows认证,提示让输入用户名和密码。


    • 已编辑 xiaoa_m 2012年4月29日 1:42 错了
    2012年4月29日 1:41

答案

  • 看看这个类似的问题:http://stackoverflow.com/questions/2520345/set-proxy-credential-in-web-browser-control

    这是其详细代码:

    var credentialStringValue = "user:pass";
    var credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue);
    var credentialBase64String = Convert.ToBase64String(credentialByteArray);
    
    Object nullObject = 0;
    Object nullObjectString = "";
    Object authObject = string.Format("Proxy-Authorization: Basic {0}{1}", credentialBase64String, Environment.NewLine);
    
    browser.Navigate(args.Url, ref nullObject, ref nullObject, ref nullObjectString, ref authObject);

    希望有用。


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年4月30日 11:35
    版主