PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSWebBrowserWithProxy/WebBrowserWithProxy.cs

#
C# | 106 lines | 71 code | 11 blank | 24 comment | 11 complexity | 2bf0c819f8f87e66a579878c7493823c MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. Module Name: WebBrowserWithProxy.cs
  3. Project: CSWebBrowserWithProxy
  4. Copyright (c) Microsoft Corporation.
  5. This WebBrowserWithProxy class inherits WebBrowser class and has a feature
  6. to set proxy server.
  7. This source is subject to the Microsoft Public License.
  8. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  9. All other rights reserved.
  10. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  11. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  12. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  13. \***************************************************************************/
  14. using System;
  15. using System.Text;
  16. using System.Windows.Forms;
  17. using Microsoft.Win32;
  18. using System.Security.Permissions;
  19. using System.ComponentModel;
  20. namespace CSWebBrowserWithProxy
  21. {
  22. public class WebBrowserWithProxy : WebBrowser
  23. {
  24. InternetProxy proxy = InternetProxy.NoProxy;
  25. // The proxy server to connect.
  26. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  27. [Browsable(false)]
  28. public InternetProxy Proxy
  29. {
  30. get
  31. {
  32. return proxy;
  33. }
  34. set
  35. {
  36. var newProxy = InternetProxy.NoProxy;
  37. if (value != null)
  38. {
  39. newProxy = value;
  40. }
  41. if (!proxy.Equals(newProxy))
  42. {
  43. proxy = newProxy;
  44. if (proxy != null && !string.IsNullOrEmpty(proxy.Address))
  45. {
  46. WinINet.SetConnectionProxy(false, proxy.Address);
  47. }
  48. else
  49. {
  50. WinINet.RestoreSystemProxy();
  51. }
  52. }
  53. }
  54. }
  55. [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
  56. public WebBrowserWithProxy()
  57. {
  58. }
  59. /// <summary>
  60. /// Wrap the method Navigate and set the Proxy-Authorization header if needed.
  61. /// </summary>
  62. [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
  63. public void Goto(string url)
  64. {
  65. System.Uri uri = null;
  66. bool result = System.Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri);
  67. if (!result)
  68. {
  69. throw new ArgumentException("The url is not valid. ");
  70. }
  71. // If the proxy contains user name and password, then set the Proxy-Authorization
  72. // header of the request.
  73. if (Proxy != null && !string.IsNullOrEmpty(Proxy.UserName)
  74. && !string.IsNullOrEmpty(Proxy.Password))
  75. {
  76. // This header uses Base64String to store the credential.
  77. var credentialStringValue = string.Format("{0}:{1}",
  78. Proxy.UserName, Proxy.Password);
  79. var credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue);
  80. var credentialBase64String = Convert.ToBase64String(credentialByteArray);
  81. string authHeader = string.Format("Proxy-Authorization: Basic {0}",
  82. credentialBase64String);
  83. Navigate(uri, string.Empty, null, authHeader);
  84. }
  85. else
  86. {
  87. Navigate(uri);
  88. }
  89. }
  90. }
  91. }