0

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

Community
  • 1
  • 1
Tobias Mayr
  • 196
  • 4
  • 16
  • what part of http://stackoverflow.com/a/38058475/361714 can't u use in VB console app? – Tom Dec 28 '16 at 23:40
  • I don't know how to fix the following line: `nst.Init(source, dest, t, e.Mime, 0, null, persist, false)`. How to I get the e.Mime since I don't use Gecko.LauncherDialogEvent e. I could adjust all the other code but this one line. I want to create a variable with that type (`Dim mimetype As nsIMIMEInfo`) but I don't know how to get there. – Tobias Mayr Dec 29 '16 at 17:38
  • Or in other words: **How to get from a String "application/pdf" to nsIMIMEInfo.** This makes me more trouble that it should. – Tobias Mayr Dec 29 '16 at 17:42
  • u could try implementing it yourself - Class MyMIMEInfo Implements nsIMIMEInfo ...... then just create a new instance of MyMIMEInfo – Tom Dec 29 '16 at 19:35
  • Thanks @Tom for all your input. Yes I did start creating my own nslMIMEInfo class but then I figured I might just use the WebClient and try to get to the cookies somehow. It finally worked and is much more effective than using GeckoFX – Tobias Mayr Dec 30 '16 at 03:05

0 Answers0