C# WebBrowser Control Proxy

前端 未结 1 747
孤城傲影
孤城傲影 2020-12-01 05:38

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 wh

相关标签:
1条回答
  • 2020-12-01 06:09
    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

    0 讨论(0)
提交回复
热议问题