WebBrowser control with an automated proxy login
I have a windows form application that contains a WebBro开发者_StackOverflowwser
control. The idea is for the WebBrowser
to surf websites without user interaction. The WebBrowser
goes through a proxy to access the internet.
I can see the requests coming through on the proxy, but they are being denied because it fails proxy authentication.
I have added the Proxy-Authorization: Basic
header. This works for a normal http page but it does not seem to work 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);
Where ws
is equal to new WebBrowser()
. The credentials are correct, because it works when I do it manually.
Any idea as to how I can programatically Authenticate the proxy credentials?
// 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;
None of those are going to work. Because of windows security features, it will always pop up the username and password dialog. You first have to store the credentials in windows credentials. the first thing you need to do is download the CredentialManagement package via NuGet Package manager. You first have to store the proxy information in the registry, with username and password. Here is the code for the registry
[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);
}
then you need to set the credentials
Credential credentials= new Credential
{
Username = "Usernmae",
Password = "Password",
Target = "Target (usualy proxy domain)",
Type = CredentialType.Generic,
PersistanceType = PersistanceType.Enterprise
};
credentials.Save();
I use this with .NET 4.5.2
There is a solution posted here:
http://www.journeyintocode.com/2013/08/c-webbrowser-control-proxy.html
It uses winnet.dll
as well as a couple of interfaces on the WebBrowser
class, including IAuthenticate
.
I havent been able to try it but looks promising.
精彩评论