C# 浏览器控制代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9035911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:42:32  来源:igfitidea点击:

C# WebBrowser Control Proxy

c#proxywebbrowser-control

提问by xhah730

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

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

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

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

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

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

采纳答案by Nick Bray

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

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