C# WebBrowser Control Proxy

2019-01-08 15:55发布

问题:

How to implement Proxy in C# WebBrowser control/Component.

What I want to know, is how to implement proxy, so my C# webBrowser control use this proxy for browsing when its run.

I also don't want to change proxy through registry ... because it affect my normal Browsing...

回答1:

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;
            }
        }

You'll have to play with it a little, but I was able to browse around the site.

Or you can try modifying the WebRequest.DefaultWebProxy setting: http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx