Programmatically download file (PDF) in GeckoFX VB.NET console application

969 views Asked by At

I use GeckoFX to navigate to a certain website which requires me to login. So far the navigating and logging in looks something like this:

Public Flag_Completed As Boolean = False

....

Navigate("http://www.website.com/loginpage/")
While Not Flag_Completed
   Application.DoEvents()
End While
Dim User As GeckoInputElement = GeckoWebBrowser1.Document.GetHtmlElementById("edit-name")
Dim Pass As GeckoInputElement = GeckoWebBrowser1.Document.GetHtmlElementById("edit-pass")
User.Value = "myUsername"
Pass.Value = "myPassword"
Dim Form = CType(GeckoWebBrowser1.Document.Forms(1), GeckoFormElement)
Form.submit()
Flag_Completed = false
While Not Flag_Completed
   Application.DoEvents()
End While

...

Public Sub Navigate(ByVal URL As String, Optional ByVal LimitTimeinMinutes As Integer = 1)
        Flag_Completed = False
        GeckoWebBrowser1.Width = 1920
        Application.DoEvents()
        Try
            GeckoWebBrowser1.Navigate(URL)
        Catch ex As Exception

        End Try

    End Sub

Everything seems to work so far but I can't find a way to download the file properly. I tried using WebClient in combination with the DownloadFile() Method like so:

Dim myWebClient As New WebClient()
myWebClient.DownloadFile("http://www.website.com/path/file.pdf", "C:\Path\to\local\file.pdf")

The problem is that the WebClient is not logged in (in contrary to my GeckoFX browser (GeckoWebBrowser1). What happens is that I end up with a file that has a .pdf file extension but opening it in a text editor makes clear that the file is actually the HTML webpage that would show up on the screen when you enter the link without being logged-in. (Makes sense)

Unfortunately I have searched for more than a day now and can't find an answer to my particular problem. There doesn't seem to be a method within the GeckoFX library what could take the DownloadFile()-method's place. I found the following question on here: How to handle downloading in GeckoFX 29 which seems to be similar to my problem. Sadly for me, the solution is aimed at a Windows.Forms application in C# and I can't seem to make use of that in my own VB.NET Console-Application. Would this be the right way to approach this? If yes, any ideas? If not, how exactly?

UPDATE: For completeness sake I will mention that I solved my particular problem (downloading a PDF behind a login) but I didn't use GeckoFX but instead used a WebClient and HttpRequests to download the file. I highly recommend the following Tutorial that explains just that: http://odetocode.com/Articles/162.aspx

0

There are 0 answers