C#WebBrowser控件代理
本文关键字:代理 控件 C#WebBrowser | 更新日期: 2023-09-27 18:21:32
如何在C#WebBrowser控件/组件中实现Proxy。
我想知道的是,如何实现代理,所以我的C#webBrowser控件在运行时使用这个代理进行浏览。
我也不想通过注册表更改代理。。。因为它影响了我的正常浏览。。。
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;
}
}
你得玩一玩,但我可以浏览一下网站。
或者,您可以尝试修改WebRequest.DefaultWebProxy设置:http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx