2012-01-27 32 views
15

Cách triển khai Proxy trong C# WebBrowser control/Component.C# WebBrowser Control Proxy

Điều tôi muốn biết, là cách triển khai proxy, do đó, điều khiển trình duyệt web C# của tôi sử dụng proxy này để duyệt khi chạy.

Tôi cũng không muốn thay đổi Proxy thông qua registry ... bởi vì nó ảnh hưởng đến Browsing bình thường của tôi ...

+0

thể trùng lặp của [Làm thế nào để thiết lập một proxy cho WebBrowser kiểm soát mà không ảnh hưởng SYSTEM/IE Proxy] (http://stackoverflow.com/questions/2499568/how-to-set-a-proxy-cho-webbrowser-control-without-effecting-the-system-ie-proxy) –

Trả lời

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

Bạn sẽ phải chơi với nó một chút, nhưng tôi đã có thể duyệt xung quanh trang web.

Hoặc bạn có thể thử thay đổi các thiết lập WebRequest.DefaultWebProxy: http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx

+0

Vậy thì sao? Đánh chặn tất cả các sự kiện điều hướng để sử dụng 'HttpWebRequest' của riêng bạn một lần nữa? –

+2

Tôi không nghĩ rằng bạn có thể đặt WebRequest.DefaultWebProxy, nhưng bạn có thể tự mình chặn nó và tạo điều hướng của riêng bạn. –

+1

cảm ơn bạn ... nó rất hữu ích đối với tôi – xhah730

Các vấn đề liên quan