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