(MVC) MVC (2018)

SetProxy for CefSharp.Winforms.ChromiumWebBrowser.

This is simple description how to set proxy to bot, based on CefBrowser.

Firstly, you see below a form from CefSharp.Winforms.ChromiumWebBrowser minimal example on VB.NET (with cookies collector and script executor). with small addins - toolstrip near the bottom edge of form. In this toolstrip I've show proxy address and number of URL sequence where bot going.



This is a code, related to proxy function.


   1:  Imports CefSharp.WinForms
   2:  Imports CefSharp
   3:  Imports System.Threading.Tasks
   4:   
   5:  Partial Public Class BrowserForm
   6:      Inherits Form
   7:   
   9:      Public ReadOnly WebBrowser As ChromiumWebBrowser
  10:      Dim ProxySetting As String
  11:   
  12:      Public Sub New(ProxySetting1 As String)
  13:          InitializeComponent()
  14:          Text = "about:blank"
  15:          WindowState = FormWindowState.Maximized
  16:          ProxySetting = ProxySetting1
  17:          ToolStripLabel1.Text = "Proxy=" & ProxySetting
  18:   
  19:          Dim RC1 = New RequestContext()
  20:          WebBrowser = New ChromiumWebBrowser("about:blank", RC1) With {
  21:              .Dock = DockStyle.Fill
  22:          }
  23:          toolStripContainer.ContentPanel.Controls.Add(WebBrowser)
  24:   
  29:          AddHandler WebBrowser.IsBrowserInitializedChanged, AddressOf OnIsBrowserInitializedChanged
  30:          AddHandler WebBrowser.LoadingStateChanged, AddressOf OnLoadingStateChanged
  31:          AddHandler WebBrowser.ConsoleMessage, AddressOf OnBrowserConsoleMessage
  32:          AddHandler WebBrowser.StatusMessage, AddressOf OnBrowserStatusMessage
  33:          AddHandler WebBrowser.TitleChanged, AddressOf OnBrowserTitleChanged
  34:          AddHandler WebBrowser.AddressChanged, AddressOf OnBrowserAddressChanged
  35:          AddHandler WebBrowser.FrameLoadEnd, AddressOf OnFrameLoadEnd
  36:   
  37:          Dim MyDownloadHandler = New DownloadHandler()
  38:          AddHandler MyDownloadHandler.DownloadFinish, AddressOf DownloadComplete
  39:          WebBrowser.DownloadHandler = MyDownloadHandler
  40:   
  41:          Dim bitness = If(Environment.Is64BitProcess, "x64", "x86")
  42:          Dim version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", CefSharp.Cef.ChromiumVersion, CefSharp.Cef.CefVersion, CefSharp.Cef.CefSharpVersion, bitness)
  43:          DisplayOutput(version)
  44:      End Sub
  45:   
  46:      Private Async Function SetProxyAsync(ByVal WebBrowserInstance As ChromiumWebBrowser, ByVal ProxyURL As String) As Task
  47:          Await CefSharp.Cef.UIThreadTaskFactory.StartNew(Function()
  48:                                                              Dim rc = WebBrowserInstance.GetBrowser().GetHost().RequestContext
  49:                                                              Dim v = New Dictionary(Of String, Object)()
  50:                                                              v("mode") = "fixed_servers"
  51:                                                              v("server") = ProxyURL
  52:                                                              Dim Error1 As String
  53:                                                              Dim success As Boolean = rc.SetPreference("proxy", v, Error1)
  54:                                                          End Function)
  55:      End Function
  56:   
  57:   
  86:      Dim CurrentUrlIndex As Integer
  87:      Dim RequestToLoadImage As Boolean = False
  88:      Public IsLogin As Boolean = False
  90:      Private Sub OnFrameLoadEnd(sender As Object, e As FrameLoadEndEventArgs)
  91:          Dim task As Task = New Task(Sub()
  93:                                          If e.Url.Contains("upwork") And e.HttpStatusCode = 200 Then
  94:                                              'WebBrowser.ViewSource()
  95:                                              If LoadForm_Instance.UrlList IsNot Nothing Then
  96:                                                  WebBrowser.GetSourceAsync().ContinueWith(Function(taskHtml)
  97:                                                                                               If taskHtml.Result IsNot Nothing Then
  98:                                                                                                   SaveUpworkToTempDB(taskHtml.Result, CurrentUrlIndex, e.Url)
  99:                                                                                               End If
 101:                                                                                               If CurrentUrlIndex < LoadForm_Instance.UrlList.Count - 1 Then
 102:                                                                                                   WebBrowser.Load(LoadForm_Instance.UrlList(CurrentUrlIndex).URL)
 103:                                                                                                   Application.DoEvents()
 105:                                                                                                   CurrentUrlIndex += 1
 106:                                                                                               End If
 107:                                                                                           End Function
 108:                                                  )
 109:                                              End If
 110:   
 111:                                          End If
 112:                                      End Sub)
 113:          task.Start()
 114:      End Sub
 115:   
 194:   
 195:      Private Sub OnIsBrowserInitializedChanged(ByVal sender As Object, ByVal e As IsBrowserInitializedChangedEventArgs)
 196:          If e.IsBrowserInitialized Then
 197:              Dim b = (CType(sender, ChromiumWebBrowser))
 198:              Me.InvokeOnUiThreadIfRequired(Function() b.Focus())
 199:              Dim Tsk1 As Task = SetProxyAsync(WebBrowser, ProxySetting.ToLower.Replace("http://", ""))
 200:          End If
 201:      End Sub
 202:   
 221:      Private Sub OnBrowserAddressChanged(ByVal sender As Object, ByVal args As AddressChangedEventArgs)
 222:          Me.InvokeOnUiThreadIfRequired(Sub() urlTextBox.Text = args.Address)
 223:          Me.InvokeOnUiThreadIfRequired(Sub() BrowserForm_Instance.Text = args.Address)
 224:          Me.InvokeOnUiThreadIfRequired(Sub() ToolStripLabel2.Text = LoadForm_Instance.UrlList(CurrentUrlIndex).i)
 225:   
 226:          Threading.Thread.Sleep(LoadForm_Instance.DelayNumericUpDown1.Value * 1000)
 227:      End Sub
 228:   
 229:      Private Sub SetCanGoBack(ByVal canGoBack As Boolean)
 230:          Me.InvokeOnUiThreadIfRequired(Sub() backButton.Enabled = canGoBack)
 231:      End Sub
 232:   
 334:  End Class

Left form for the top of this page I use to manage proxy servers list, and for call CefBrowser I use another tab.



and this code:


 413:      Private Sub UpworkBootButton_Click(sender As Object, e As EventArgs) Handles UpworkBootButton.Click
 414:          LoginUpdate()
 415:          '
 416:          Try
 417:              '
 418:              LoadForm_Instance.Refresh()
 419:              Application.DoEvents()
 420:              Dim Proxies = (From X In db1.ProxyTabs Select X Order By X.CrDate Descending).ToList
 421:              BrowserForm_Instance = New BrowserForm(ProxyComboBox1.Text)
 422:              BrowserForm_Instance.FormBorderStyle = FormBorderStyle.FixedDialog
 423:              BrowserForm_Instance.MaximumSize = New Size(500, 700)
 424:              BrowserForm_Instance.Show()
 425:              If UrlList IsNot Nothing Then
 426:                  If UrlList.Count > 0 Then
 427:                      BrowserForm_Instance.WebBrowser.Load(UrlList(0).URL)
 428:                  End If
 429:              End If
 430:              '
 431:          Catch ex As Exception
 432:              MsgBox(ex.Message)
 433:          End Try


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/CefSharp_ChromiumWebBrowser_SetProxy/index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>