我在使用wininet设置代理用户名和密码时遇到问题。
以下代码设置代理,但用户名和密码失败,错误代码为“12018”
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
private void RefreshIESettings(string strProxy,string proxyUsername,string proxyPassword) {
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
const int INTERNET_OPTION_PROXY_USERNAME = 43;
const int INTERNET_OPTION_PROXY_PASSWORD = 44;
IntPtr username = Marshal.StringToHGlobalAnsi(proxyUsername);
IntPtr password = Marshal.StringToHGlobalAnsi(proxyPassword);
INTERNET_PROXY_INFO struct_IPI;
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
Marshal.GetNativeVariantForObject(proxyUsername, username);
Marshal.GetNativeVariantForObject(proxyPassword, password);
bool resultF = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, username, proxyUsername.Length);
var errorF = Marshal.GetLastWin32Error();
//-- Set Proxy Password
bool resultG = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, password, proxyPassword.Length + 1);
var errorG = Marshal.GetLastWin32Error();
答案 0 :(得分:0)
代理用户名和密码选项需要来自InternetOpen调用的有效hInternet句柄,这是我无法从Web浏览器控件中提取的。
最终我必须挂钩win32 WM_INITDIALOG消息遍历子窗口对象,直到找到用户名,密码和ok元素,自动填充提供的凭据然后提交。
非常hackish,但它的确有效。我无法获得IAuthenticate.Authenticate函数来执行此特定代理凭据请求,似乎只调用了基本的Windows身份验证提示。
请参阅以下内容:
Set Proxy Credential in Web Browser Control
How to set a proxy for Webbrowser Control without effecting the SYSTEM/IE proxy