18

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...

Taryn
  • 242,637
  • 56
  • 362
  • 405
xhah730
  • 215
  • 1
  • 2
  • 5
  • possible duplicate of [How to set a proxy for Webbrowser Control without effecting the SYSTEM/IE proxy](http://stackoverflow.com/questions/2499568/how-to-set-a-proxy-for-webbrowser-control-without-effecting-the-system-ie-proxy) – Sheng Jiang 蒋晟 Jan 29 '12 at 04:42

1 Answers1

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

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

Nick Bray
  • 1,953
  • 12
  • 18
  • Then what? Intercept all navigation events to use your own `HttpWebRequest` again? – Edward Thomson Jan 27 '12 at 16:27
  • 2
    I don't think you can set WebRequest.DefaultWebProxy, but you can intercept it yourself and create your own navigation. – Nick Bray Jan 27 '12 at 17:22
  • 4
    Good technique (I upvoted) - however this does not cover postbacks and cookies – WantToBeAnonomous Jun 11 '13 at 19:57
  • @NickBray will this solution work if the web page being requested has a lot of images, scripts, etc, which need to pass through the proxy server? How does one attempt to do an end-2-end test of this solution? – deostroll Jun 12 '15 at 06:02
  • This worked great for me to load the initial page, but when using `Document.GetElementById("submit").InvokeMember("click")` it won't load the next page. The `currentUri` variable is set to blank and the screen just displays a white page. Is there a way to capture the stream from clicking the button? – Sean Perryman Oct 01 '15 at 22:34
  • 2
    the problem is, document is loaded using the proxy stream but,,, rest of its contents like js files and images or even a frame url is loaded using non proxy connection. i am also looking for a work around, but it seems the control itself is made not to have individual settings or proxy but to use what ever the real ie supplies.... – Beep.exe Oct 16 '15 at 22:38