C#WebBrowser控制代理

时间:2012-01-27 15:40:14

标签: c# proxy webbrowser-control

如何在C#WebBrowser控件/组件中实现代理。

我想知道的是,如何实现代理,所以我的C#webBrowser控件在运行时使用此代理进行浏览。

我也不想通过注册表更改代理...因为它会影响我的正常浏览...

1 个答案:

答案 0 :(得分:15)

private Uri currentUri;

        private void Form1_Load(object sender, EventArgs e)
        {
            currentUri = new Uri(@"http://www.stackoverflow.com");
            HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.stackoverflow.com");
            //WebProxy myProxy = new WebProxy("208.52.92.160:80");
            //myRequest.Proxy = myProxy;

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            webBrowser1.DocumentStream = myResponse.GetResponseStream();

            webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
        }

        void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.AbsolutePath != "blank")
            {
                currentUri = new Uri(currentUri, e.Url.AbsolutePath);
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri);

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                webBrowser1.DocumentStream = myResponse.GetResponseStream();
                e.Cancel = true;
            }
        }

你必须稍微玩一下,但我能浏览网站。

或者您可以尝试修改WebRequest.DefaultWebProxy设置: http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx