2012-01-27 10 views
15

Come implementare il proxy in C# Controllo/componente WebBrowser.C# Proxy controllo browser Web

Quello che voglio sapere, è come implementare il proxy, quindi il mio controllo webBrowser C# usa questo proxy per la navigazione quando è in esecuzione.

Anche io non voglio cambiare procura attraverso registro ... perché influisce sul mio normale navigazione ...

+0

possibile duplicato di [Come impostare un proxy per il browser web di controllo senza influenzare il SISTEMA/IE Proxy] (http://stackoverflow.com/questions/2499568/how-to-set-a-proxy-for-webbrowser-control-senza-effect-the-system-ie-proxy) –

risposta

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

Dovrete giocare con un po ', ma sono riuscito a navigare nel sito.

Oppure si può provare a modificare l'impostazione WebRequest.DefaultWebProxy: http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx

+0

E allora? Intercettare tutti gli eventi di navigazione per utilizzare nuovamente il proprio 'HttpWebRequest'? –

+2

Non penso che tu possa impostare WebRequest.DefaultWebProxy, ma puoi intercettarlo tu stesso e creare la tua navigazione. –

+1

grazie ... è molto utile per me – xhah730

Problemi correlati