C# 浏览器控制代理 [英] C# WebBrowser Control Proxy
问题描述
如何在 C# WebBrowser 控件/组件中实现代理.
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.
我也不想通过注册表更改代理...因为它会影响我的正常浏览...
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.
或者您可以尝试修改 WebRequest.DefaultWebProxy 设置:http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx
Or you can try modifying the WebRequest.DefaultWebProxy setting: http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx
这篇关于C# 浏览器控制代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!