C#WebBrowser控件代理

C#WebBrowser控件代理

本文介绍了C#WebBrowser控件代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


解决方案

 私人乌里currentUri;        私人无效Form1_Load的(对象发件人,EventArgs的发送)
        {
            currentUri =新的URI(@http://www.stackoverflow.com);
            HttpWebRequest的myRequest =(HttpWebRequest的)HttpWebRequest.Create(http://www.stackoverflow.com);
            // WebProxy MYPROXY =新WebProxy(208.52.92.160:80);
            //myRequest.Proxy = MYPROXY;            HttpWebResponse myResponse =(HttpWebResponse)myRequest.GetResponse();            webBrowser1.DocumentStream = myResponse.GetResponseStream();            webBrowser1.Navigating + =新WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
        }        无效webBrowser1_Navigating(对象发件人,WebBrowserNavigatingEventArgs E)
        {
            如果(e.Url.AbsolutePath!=空白)
            {
                currentUri =新的URI(currentUri,e.Url.AbsolutePath);
                HttpWebRequest的myRequest =(HttpWebRequest的)HttpWebRequest.Create(currentUri);                HttpWebResponse myResponse =(HttpWebResponse)myRequest.GetResponse();                webBrowser1.DocumentStream = myResponse.GetResponseStream();
                e.Cancel =真;
            }
        }

您将有与它一点点打,但我能够浏览在工地附近。

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

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

解决方案
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

这篇关于C#WebBrowser控件代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:52