Visual Basic programmatically transfers the username and password to the https url to make the web page of the web browser, as well as download from the web page

With plain HTTP, I can download the download and go to the routers, but I cannot find any code to do this when the routers are on HTTPS.

For download I use this:

Try
    My.Computer.Network.DownloadFile("http://" & "180.29.74.70" & "/cgi-bin/log.cgi", "C:\Users\ssb\Desktop\randomword.txt", "username", "password")
    WebBrowser1.Refresh()
Catch ex As Exception
    MessageBox.Show("Router not sufficient for operation Return for Inspection cannot download log file")
End Try

To upload a file, I use this:

  My.Computer.Network.UploadFile("C:\Users\ssb\Desktop\tomtn.txt", "http://" & "180.29.74.70" & "/cgi-bin/updateconfig.cgi", "username", "password")

To go to a webpage over HTTP, I use this:

WebBrowser1.Navigate("https://username:password@180.29.74.70 ")

But when I use HTTPS:

WebBrowser1.Navigate("https://username:password@180.29.74.70 ")

I get this warning:

Certificate error

Then I click “Yes” and it goes to the page, but I need the code to get around any security issues like these.

+4
source share
3 answers

, , - https-url , -

2

, cerareate ,

, ,

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'check if a simular file doesnt exists so you can create a new file and deletes the file if it exists
        If File.Exists("C:\pathtoyourfile\yourfilename.txt") Then
            File.Delete("C:\pathtoyourfile\yourfilename.txt")
        End If

        'Type this before your download or hhtps request
        'ByPass SSL Certificate Validation Checking
        System.Net.ServicePointManager.ServerCertificateValidationCallback =
  Function(se As Object,
  cert As System.Security.Cryptography.X509Certificates.X509Certificate,
  chain As System.Security.Cryptography.X509Certificates.X509Chain,
  sslerror As System.Net.Security.SslPolicyErrors) True

        'Call web application/web service with HTTPS URL here
        '=========================================================================================
        'ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications
        Try
            My.Computer.Network.DownloadFile("https://176.53.78.22/filenameonserveryouwanttodownload", "C:\pathtoyourfile\yourfilename.txt", "Yourusername", "yourpassword")
            WebBrowser1.Refresh()
        Catch ex As Exception
            MessageBox.Show("message saying something didnt work")
            'exit sub if it worked
            Exit Sub
        End Try
        MessageBox.Show(" message saying it worked")
        '=========================================================================================
        'Restore SSL Certificate Validation Checking
        System.Net.ServicePointManager.ServerCertificateValidationCallback = Nothing
    End Sub

, -, , "", -

WebBrowser1.Navigate("https://username:password@180.29.74.70 ")
0

, , .

  • , WebBrowser HTTPS?
  • , DownloadFile() HTTPS?

-, . , URL- HTTPS, , , .

, URL-, , SSL WebBrowser.

, (, VB) :

Partial Public Class Form1
  Inherits Form

  Private WithEvents WebBrowser As New WebBrowser

  Private Sub WebBrowser_DocumentCompleted(Sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
    If e.Url.ToString() = "about:blank" Then
      'create a certificate mismatch
      WebBrowser.Navigate("https://74.125.225.229/")
    End If
  End Sub
End Class



<Guid("6D5140C1-7436-11CE-8034-00AA006009FA")>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
<ComImport>
Public Interface UCOMIServiceProvider
  <PreserveSig>
  Function QueryService(<[In]> ByRef guidService As Guid, <[In]> ByRef riid As Guid, <Out> ByRef ppvObject As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface



<ComImport>
<ComVisible(True)>
<Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b")>
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IWindowForBindingUI
  <PreserveSig>
  Function GetWindow(<[In]> ByRef rguidReason As Guid, <[In], Out> ByRef phwnd As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface



<ComImport>
<ComVisible(True)>
<Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")>
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IHttpSecurity
  'derived from IWindowForBindingUI
  <PreserveSig>
  Function GetWindow(<[In]> ByRef rguidReason As Guid, <[In], Out> ByRef phwnd As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer
  <PreserveSig>
  Function OnSecurityProblem(<[In], MarshalAs(UnmanagedType.U4)> dwProblem As UInteger) As Integer
End Interface



Public Class MyWebBrowser
  Inherits WebBrowser
  Public Shared IID_IHttpSecurity As New Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")
  Public Shared IID_IWindowForBindingUI As New Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b")
  Public Const S_OK As Integer = 0
  Public Const S_FALSE As Integer = 1
  Public Const E_NOINTERFACE As Integer = &H80004002
  Public Const RPC_E_RETRY As Integer = &H80010109



  Protected Overrides Function CreateWebBrowserSiteBase() As WebBrowserSiteBase
    Return New MyWebBrowserSite(Me)
  End Function



  Private Class MyWebBrowserSite
    Inherits WebBrowserSite

    Implements UCOMIServiceProvider
    Implements IHttpSecurity
    Implements IWindowForBindingUI

    Private myWebBrowser As MyWebBrowser



    Public Sub New(myWebBrowser As MyWebBrowser)
      MyBase.New(myWebBrowser)
      Me.myWebBrowser = myWebBrowser
    End Sub



    Public Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As IntPtr) As Integer Implements UCOMIServiceProvider.QueryService
      If riid = IID_IHttpSecurity Then
        ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(IHttpSecurity))
        Return S_OK
      End If
      If riid = IID_IWindowForBindingUI Then
        ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(IWindowForBindingUI))
        Return S_OK
      End If
      ppvObject = IntPtr.Zero
      Return E_NOINTERFACE
    End Function



    Public Function GetWindow(ByRef rguidReason As Guid, ByRef phwnd As IntPtr) As Integer Implements IHttpSecurity.GetWindow, IWindowForBindingUI.GetWindow
      If rguidReason = IID_IHttpSecurity OrElse rguidReason = IID_IWindowForBindingUI Then
        phwnd = myWebBrowser.Handle
        Return S_OK
      Else
        phwnd = IntPtr.Zero
        Return S_FALSE
      End If
    End Function



    Public Function OnSecurityProblem(dwProblem As UInteger) As Integer Implements IHttpSecurity.OnSecurityProblem
      'ignore errors
      'undocumented return code, does not work on IE6
      Return S_OK
    End Function
  End Class
End Class

№ 2: , WebBrowser DownloadFile(). , , , WebBrowser . :

Partial Public Class Form2
  Inherits Form

  Private Sub WebBrowser_Navigating(Sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser.Navigating
    Dim sFilePath As String
    Dim oClient As Net.WebClient

    ' This can be any conditional criteria you wish '
    If (e.Url.Segments(e.Url.Segments.Length - 1).EndsWith(".pdf")) Then
      SaveFileDialog.FileName = e.Url.Segments(e.Url.Segments.Length - 1)
      e.Cancel = True

      If SaveFileDialog.ShowDialog() = DialogResult.OK Then
        sFilePath = SaveFileDialog.FileName
        oClient = New Net.WebClient

        AddHandler oClient.DownloadFileCompleted, New AsyncCompletedEventHandler(AddressOf DownloadFileCompleted)

        oClient.DownloadFileAsync(e.Url, sFilePath)
      End If
    End If
  End Sub



  Private Sub DownloadFileCompleted(Sender As Object, e As AsyncCompletedEventArgs)
    MessageBox.Show("File downloaded")
  End Sub



  Private WithEvents SaveFileDialog As New SaveFileDialog
  Private WithEvents WebBrowser As New WebBrowser
End Class

, URL .

+1

:

[...] , , .

, " SSL", , , : VB.net SSL, .

slaks :

In VB.Net you need to write:

ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications

0
source

Source: https://habr.com/ru/post/1674066/


All Articles