我有一个包含WebBrowser控件的windows窗体应用程序。这个想法是让WebBrowser在没有用户交互的情况下浏览网站。WebBrowser通过代理来访问互联网。
我可以看到请求在代理上通过,但它们被拒绝了,因为它没有通过代理身份验证。
我已经添加了Proxy-Authorization: Basic标头。这对普通的http页面有效,但似乎不起作用。
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()。凭据是正确的,因为它在我手动操作时有效。
关于如何通过编程验证代理凭据,您有什么想法吗?
发布于 2011-05-31 16:24:45
// 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; 发布于 2016-03-13 09:24:36
这些都行不通。由于windows的安全功能,它将始终弹出用户名和密码对话框。您首先必须将凭据存储在windows凭据中。您需要做的第一件事是通过NuGet包管理器下载CredentialManagement包。您首先必须将代理信息存储在注册表中,并提供用户名和密码。以下是注册表的代码
[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);
}然后您需要设置凭据
Credential credentials= new Credential
{
Username = "Usernmae",
Password = "Password",
Target = "Target (usualy proxy domain)",
Type = CredentialType.Generic,
PersistanceType = PersistanceType.Enterprise
};
credentials.Save();我在.NET 4.5.2中使用了这个
发布于 2013-12-08 19:00:41
这里有一个解决方案:
http://www.journeyintocode.com/2013/08/c-webbrowser-control-proxy.html
它使用winnet.dll以及WebBrowser类上的几个接口,包括IAuthenticate。
我还没能试过,但看起来很有希望。
https://stackoverflow.com/questions/6184675
复制相似问题