첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 웹 브라우저에서 프록시 서버 사용하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using Microsoft.Win32;

using System;

using System.Runtime.InteropServices;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 인터넷 옵션 설정하기 - InternetSetOption(internetHandle, option, bufferHandle, bufferLength)

 

        /// <summary>

        /// 인터넷 옵션 설정하기

        /// </summary>

        /// <param name="internetHandle">인터넷 핸들</param>

        /// <param name="option">옵션</param>

        /// <param name="bufferHandle">버퍼 핸들</param>

        /// <param name="bufferLength">버퍼 길이</param>

        /// <returns>처리 결과</returns>

        [DllImport("wininet.dll")]

        private static extern bool InternetSetOption(IntPtr internetHandle, int option, IntPtr bufferHandle, int bufferLength);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 레지스트리

        /// </summary>

        private RegistryKey registry = Registry.CurrentUser.OpenSubKey

        (

            "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",

            true

        );

 

        /// <summary>

        /// INTERNET_OPTION_SETTINGS_CHANGED

        /// </summary>

        private const int INTERNET_OPTION_SETTINGS_CHANGED = 39;

 

        /// <summary>

        /// INTERNET_OPTION_REFRESH

        /// </summary>

        private const int INTERNET_OPTION_REFRESH = 37;

 

        /// <summary>

        /// Proxy URL

        /// </summary>

        private string proxyURL = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load                                          += Form_Load;

            this.addressTextBox.KeyPress                  += addressTextBox_KeyPress;

            this.goButton.Click                           += goButton_Click;

            this.proxyServerComboBox.SelectedIndexChanged += proxyServerComboBox_SelectedIndexChanged;

            this.setupButton.Click                        += setupButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            this.proxyServerComboBox.Text = "OFF";

        }

 

        #endregion

        #region 주소 텍스트 박스 키 PRESS 처리하기 - addressTextBox_KeyPress(sender, e)

 

        /// <summary>

        /// 주소 텍스트 박스 키 PRESS 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void addressTextBox_KeyPress(object sender, KeyPressEventArgs e)

        {

            if(e.KeyChar == (char)13)

            {

                this.webBrowser.Navigate(this.addressTextBox.Text);

            }

        }

 

        #endregion

        #region 이동 버튼 클릭시 처리하기 - goButton_Click(sender, e)

 

        /// <summary>

        /// 이동 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void goButton_Click(object sender, EventArgs e)

        {

            this.webBrowser.Navigate(this.addressTextBox.Text);

        }

 

        #endregion

        #region 프록시 서버 콤보 박스 선택 인덱스 변경시 처리하기 - proxyServerComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 프록시 서버 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void proxyServerComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            try

            {

                this.proxyServerComboBox.SelectedIndexChanged -= proxyServerComboBox_SelectedIndexChanged;

 

                if(this.proxyServerComboBox.Text == "ON")

                {

                    if(this.proxyURL != null)

                    {

                        registry.SetValue("ProxyServer", this.proxyURL);

                        registry.SetValue("ProxyEnable", 1);

 

                        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);

 

                        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

                    }

                    else

                    {

                        this.proxyServerComboBox.Text = "OFF";

                    }

                }

                else

                {

                    registry.SetValue("ProxyServer", "");

                    registry.SetValue("ProxyEnable", 0 );

 

                    InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);

 

                    InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

                }

            }

            finally

            {

                this.proxyServerComboBox.SelectedIndexChanged += proxyServerComboBox_SelectedIndexChanged;

            }

        }

 

        #endregion

        #region 설정 버튼 클릭시 처리하기 - setupButton_Click(sender, e)

 

        /// <summary>

        /// 설정 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void setupButton_Click(object sender, EventArgs e)

        {

            SettingForm form = new SettingForm();

 

            form.ShowDialog(this);

 

            this.proxyURL = form.ProxyServerURL;

 

            if (this.proxyURL == null)

            {

                this.proxyServerComboBox.Text = "OFF";

            }

            else

            {

                this.proxyServerComboBox.Text = "ON";

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요