vb.net downloading a file without popups - vb.net

I am trying to download tracking information from FedEx's website. First I have to pass login credentials and then navigate to a webpage which triggers an automatic download. My code so far works fine for this. However; as soon as the download is triggered a popup comes up asking if I want to save the file and then when I click save I asks where I want to save the file to. I want to disable the popups and have the file just automatically saved in my downloads folder. Is there a way to do this? Thanks in advance for any help!
Here is my code so far.
Dim WebBrowser1 As New WebBrowser
Dim url As String = "https://www.fedex.com/insight/manifest/manifest.jsp?&__QS=252D0F3B4E380B211B122B1A09251510050E0F5C273A223E34360539237976645E45745E57776C&"
WebBrowser1.Navigate(url)
WebBrowser1.ScriptErrorsSuppressed = True
Threading.Thread.Sleep(2000)
Application.DoEvents()
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "MyUsername")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "MyP#ssw0rd")
WebBrowser1.Document.GetElementById("login").InvokeMember("click")
Threading.Thread.Sleep(1000)
Application.DoEvents()
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
WebBrowser1.Navigate("https://www.fedex.com/insight/manifest/download_post.jsp?VIEW=|Outbound_View&INFOTYPE=STATUS")
Do Until WebBrowser1.IsBusy = False
Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
So I tried this code but all this does is still just download the page's source.
Using client As New WebClient()
client.Headers("User-Agent") = "Mozilla/4.0"
client.Credentials = New NetworkCredential("Username", "P#sword")
client.Credentials = CredentialCache.DefaultCredentials ' << if Windows Authentication
Dim content As String = client.DownloadString("https://www.fedex.com/insight/manifest/download.jsp?VIEW=/Outbound_View")
Console.WriteLine(content.Substring(0, 15))
End Using
I checked the webpage's source again to try to find a different url but also could not find anything else. I was wondering if it would be easier to download using WebBrowser.Navigate and then suppress the popups. Is there away to suppress or get rid of the download popups in vb? I am just not having much success with the WebClient. Thanks for all the help as I am still pretty new at vb.net!

Related

File Being Used By .NET Mail

I wrote a simple program written to collect files, send the files, then delete said files. I am sending them via Email using System.Net.Mail
If Label6.Text = "" Then
mail.Attachments.Add(New Attachment(zipPath))
End If
'Enables SSL, if required
ssl = Provider.ssl
If skipAhead = True Then
ssl = True
End If
If ssl = True Then
SmtpServer.EnableSsl = True
End If
'Sends the email
SmtpServer.Send(mail)
'Allows the user to either keep testing, or quit.
If skipAhead = True Then
My.Computer.FileSystem.DeleteFile(unalteredPath)
My.Computer.FileSystem.DeleteFile(unalteredPath1)
My.Computer.FileSystem.DeleteFile(zipPath)
Else
Dim keepOpen As Integer = MsgBox("The Email was sent. You can keep testing if you would like to. Press ok to close, and cancel to keep testing", MsgBoxStyle.OkCancel)
If keepOpen = 1 Then
Close()
End If
End If
As seen in line 2, the attachment is added to the email, and I do not attempt to delete the attachment until after the email is sent, however when the code is running, it throws an error that the file is being used by another process.
I am also wondering if this could be lingering from the .zip being created itself. Here is the code that does that:
Public Sub Zipping()
'Copies files to the folder where they will be zipped from
My.Computer.FileSystem.CopyFile(unalteredPath, outputs & "\ExIpOutput.txt")
My.Computer.FileSystem.CopyFile(unalteredPath1, outputs & "\IpConfig.txt")
'Deletes the old output files
My.Computer.FileSystem.DeleteFile(unalteredPath)
My.Computer.FileSystem.DeleteFile(unalteredPath1)
'Starts the zip Sub
ZipFile.CreateFromDirectory(outputs, zipPath, CompressionLevel.Fastest, True)
My.Computer.FileSystem.DeleteDirectory(outputs, FileIO.DeleteDirectoryOption.DeleteAllContents)
End Sub
Here is the CreateFromDirectory sub:
Public Shared Sub CreateFromDirectory(sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As Compression.CompressionLevel, includeBaseDirectory As Boolean)
End Sub
Is there something I'm missing here, or do I need to have the program sleep for a bit to let the email send, then delete the .zip file?
You can load the file into an array: File.ReadAllBytes(String) Method.
Then get a MemoryStream from that: How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?
And finally, you can use a MemoryStream for an attachment: Attach a file from MemoryStream to a MailMessage in C#.
As the data to be sent is in memory, you should be able to delete the file. Note that if there is a crash, the data is gone.
The examples are in C#, but if you have problems using the methods in VB.NET, please edit your question to show how far you've got and tell us what the problem is.
A better solution to this issue is to dispose the Attachment object that is locking the file. Any object you create that has a Dispose method should have that method called when you're done with the object and an Attachment is no different.
Dim fileAttachment As Attachment
If Label6.Text = "" Then
fileAttachment = New Attachment(zipPath)
mail.Attachments.Add(fileAttachment)
End If
'...
SmtpServer.Send(mail)
If fileAttachment IsNot Nothing Then
fileAttachment.Dispose()
End If

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

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

HTTPOnly Cookies with WebBrowser and WebClient

I made a program that allows a user to login with a WebBrowser control to our CRM system (ConnectWise)
The cookies would then be moved a Web Client control, where it would parse a url which came back with a .csv file. The Webclient would then download the csv to "C:\Temp\unassigned.csv" and I would then use this file to do other things in the program.
This worked great for months, until there was an update to Connectwise which brought in the use of HTTPOnly Cookies, of which I cannot extract the cookies from the Web Browser Control. The WebBrowser1.Document.Cookie string is now blank.
Here is the code:
Private Sub Internetr()
If WebBrowser1.Url.ToString.StartsWith("REDACTED") Then 'if connectwise is logged in
Dim _url As String = "REDACTED"
Dim _filename As String = ("C:\Temp\unassigned.csv")
WebBrowser1.Visible = False 'Hide the webbrowser
Refresh.Enabled = True 'Enable the ticker
Dim csv As WebClient = New WebClient 'new download webclient
'THIS IS WHERE I MOVED THE COOKIES FROM WEB BROWSER TO WEBCLIENT BEFORE
Try
csv.DownloadFile(_url, _filename)
load.Text = "Updating List..."
csv.Dispose()
DoUnassigned()
Catch ex As Exception
load.Text = "Failed to update list, Last updated at: " & gettime
debugbtn.Visible = True
exception = ex.ToString
Failed()
End Try
Else
WebBrowser1.Visible = True 'if not logged in, show the webbrowser
End If
End Sub
Does anyone have any ideas please?

Get output of external console app to RichTextBox

I used the VB.Net Shell() command to start a console app that I haven't created. I want to get the lines from that console to a RichTextBox in my form. It should be possible because I have seen many apps that do this. Please provide some information or any program that might help me. I tried to see if the external app creates log files, but it does not.
Here's how I started. What should I add, and where, to return the output?
Try
writer.WriteLine(RichTextBox1.Text)
writer.Close()
Shell(CurDir() & "\yelloeye.exe .\new.demo")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here's an example using the RedirectStandardOutput property as mentioned by #Plutonix which will ping SO and display the results in a RichTextBox. Just replace the .exe name and arguments (if any) to suit your needs.
Private Sub ShellToRTB()
Dim p As New Process()
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "www.stackoverflow.com"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()
RichTextBox1.Text = p.StandardOutput.ReadToEnd()
p.WaitForExit()
End Sub
Result:

OpenFileDialog Filter Option Not Working

Since I put my OpenFileDialog into a BackgroundWorker, the Filter option no longer works.
I use to have this as a button click and it worked fine, with the exception of what every file I opened it wouldn't close the file, hence I added the BackgroundWorker.
Anywho, here's my current code, nothing different from the button click code I had.
Dim OpenFileDialog2 As New OpenFileDialog()
OpenFileDialog2.InitialDirectory = "C:\Temp\Config_Files\"
OpenFileDialog2.Filter = "Configuration Files (*.cfg)|*.cfg"
Is there something I need to add to make this work properly?
I think that you are misunderstanding the advice to use a backgroundworker.
You should let the OpenFileDialog do its work and grab the cfg file to be processed, then, if you want a faster UI response start the backgroundworker.
Dim fileToProcess as String = string.Empty
Using opf As New OpenFileDialog()
opf.InitialDirectory = "C:\Temp\Config_Files\"
opf.Filter = "Configuration Files (*.cfg)|*.cfg"
if opf.ShowDialog() = DialogResult.OK then
fileToProcess = opf.FileName
Endif
End Using
if fileToProcess <> string.Empty then
' Now start you backgroundworker to do its job
end if
Of course, the user could re-start againg the same code and select again the same file. This could lead to unexpected results. Better to disable the Button/Menu or whatever that start the file selection process till the previous process ends.