C# 带有自动代理登录的WebBrowser控件
我有一个windows窗体应用程序,它包含一个C# 带有自动代理登录的WebBrowser控件,c#,winforms,authentication,proxy,webbrowser-control,C#,Winforms,Authentication,Proxy,Webbrowser Control,我有一个windows窗体应用程序,它包含一个WebBrowser控件。其想法是让WebBrowser在没有用户交互的情况下浏览网站。WebBrowser通过代理访问互联网 我可以在代理上看到请求,但它们被拒绝,因为代理身份验证失败 我添加了代理授权:Basic标题。这适用于普通http页面,但在https中似乎不起作用: var credentialStringValue = "proxyUser:proxyPassword"; byte[] credentialByteArray = ASC
WebBrowser
控件。其想法是让WebBrowser
在没有用户交互的情况下浏览网站。WebBrowser
通过代理访问互联网
我可以在代理上看到请求,但它们被拒绝,因为代理身份验证失败
我添加了代理授权:Basic
标题。这适用于普通http页面,但在https中似乎不起作用:
var credentialStringValue = "proxyUser:proxyPassword";
byte[] credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue);
var credentialBase64String = Convert.ToBase64String(credentialByteArray);
string Headers = string.Format("Proxy-Authorization: Basic {0}{1}", credentialBase64String, Environment.NewLine);
ws.Navigate(url,TargetFrameName,PostData,Headers);
其中ws
等于new WebBrowser()
。凭据是正确的,因为当我手动执行此操作时,它会工作
您知道如何通过编程方式验证代理凭据吗?这里发布了一个解决方案:
// do what you want with proxy class
WebProxy webProxy = new WebProxy(host, port)
{
Credentials = ...
}
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://example.com");
webRequest.Proxy = webProxy;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream receiveStream = response.GetResponseStream();
WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentStream = receiveStream;
它使用winnet.dll
以及WebBrowser
类上的几个接口,包括iaauthenticate
我还没试过,但看起来很有希望。这些都没用。由于windows的安全功能,它将始终弹出用户名和密码对话框。您首先必须将凭据存储在windows凭据中。您需要做的第一件事是通过NuGet package manager下载包。您首先必须将代理信息存储在注册表中,包括用户名和密码。这是注册表的代码
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
static void setProxyRegistry(string proxyhost, bool proxyEnabled, string username, string password)
{
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
const string keyName = userRoot + "\\" + subkey;
Registry.SetValue(keyName, "ProxyServer", proxyhost, RegistryValueKind.String);
Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);
Registry.SetValue(keyName, "ProxyPass", password, RegistryValueKind.String);
Registry.SetValue(keyName, "ProxyUser", username, RegistryValueKind.String);
//<-loopback>;<local>
Registry.SetValue(keyName, "ProxyOverride", "*.local", RegistryValueKind.String);
// These lines implement the Interface in the beginning of program
// They cause the OS to refresh the settings, causing IP to realy update
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
我将其与.NET 4.5.2一起使用,这不是OP所要求的。他想通过代理运行整个WebBrowser。你是在建议他把一切都改写成你建议的方式吗?他也可以制作控件。@Kyle:我的建议是不使用
Navigate()
,而是通过WebProxy的WebRequest获取HTML,并使用WebBrowser控件呈现。我明白了。当有人点击网站上的链接时会发生什么?@Kyle:你说得对。作为变通方法-覆盖所有链接单击并通过代理以相同的方式执行请求。@Kyle:我在谷歌搜索到的解决OP问题的所有方法都将使用P/Invoke@Abat-当然-没有人是。下次进行双重编辑时,只需在编辑中添加原因。
Credential credentials= new Credential
{
Username = "Usernmae",
Password = "Password",
Target = "Target (usualy proxy domain)",
Type = CredentialType.Generic,
PersistanceType = PersistanceType.Enterprise
};
credentials.Save();