WebBrowser management with automatic proxy

I have a Windows form application that contains a WebBrowser . The idea is for WebBrowser browse websites without user intervention. WebBrowser goes through a proxy server to access the Internet.

I can see the requests passing through the proxy server, but they are denied because it does not allow checking the proxy server.

I added the Proxy-Authorization: Basic header. This works for a regular http page, but https doesn't seem to work:

 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 they work when I do it manually.

Any idea on how I can programmatically authenticate proxy credentials?

+8
authentication c # proxy winforms webbrowser-control
source share
3 answers
  // 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; 
+3
source share

There is a solution here:

http://www.journeyintocode.com/2013/08/c-webbrowser-control-proxy.html

It uses winnet.dll , as well as a couple of interfaces in the WebBrowser class, including IAuthenticate .

I could not try, but it looks promising.

0
source share

None of them are going to work. Due to the security features of Windows, a username and password dialog will always be displayed. You must first save the credentials in the Windows credentials. the first thing you need to do is download the CredentialManagment package through the NuGet package manager. First you need to save the proxy information in the registry with a 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 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

0
source share

All Articles