Hello there HF! -- Been a little while since I've contributed, so I figured I'd do so. I'm sure (not really) we all have been a situation where we needed to make a program that downloads and executes a file from a website.
Well that should be pretty simple, shouldn't it? Isn't that just taking the file name from the link itself by finding the last slash? E.g www.example.com/download/test.jpg
Well, this works to an extent but it requires the website to have a direct download link with the file name in the link. What if the download link looks like this: www.example.com/download.php?id=1. Well that could be a problem The file name returned when using the previous method would then be "download.php?id=1". Well, there is a solution!
It's quite simple; it just requires the use of the HttpWebRequest and HttpWebResponse classes.
First you create a URI from a string and an HttpWebRequest by doing so:
The timeout is optional of course. The TryCreate is not necessary, but it's nice to have in case there is a user that inputs the link (error checking)
Now we create a result from the request by doing so:
Note: For the sake of simplicity I use the synchronous GetResponse() method, depending on the timeout you set, the UI may freeze if you didn't. Now once that is done we have to get the file name using the HttpWebResponse object. We wll do the following:
What we are doing here is declaring a string called Filename and we assign that the LocalPath property. We then change filename to the substring after the last index of "/", this is because the LocalPath gives us the folder the file is inside on the remote server.
Now our code looks like this:
The Result (Click to View)
If you would like to download my VS Solution, you may do so here
I hope you all enjoy 🎉
Well that should be pretty simple, shouldn't it? Isn't that just taking the file name from the link itself by finding the last slash? E.g www.example.com/download/test.jpg
Well, this works to an extent but it requires the website to have a direct download link with the file name in the link. What if the download link looks like this: www.example.com/download.php?id=1. Well that could be a problem The file name returned when using the previous method would then be "download.php?id=1". Well, there is a solution!
It's quite simple; it just requires the use of the HttpWebRequest and HttpWebResponse classes.
First you create a URI from a string and an HttpWebRequest by doing so:
Code
Dim Target As Uri
If Uri.TryCreate(TextBox1.Text, UriKind.Absolute, Target) Then
Dim Request As HttpWebRequest = HttpWebRequest.Create(Target)
Request.Method = "GET"
Request.Timeout = 120000 '2 minutes timeoutThe timeout is optional of course. The TryCreate is not necessary, but it's nice to have in case there is a user that inputs the link (error checking)
Now we create a result from the request by doing so:
Code
Dim Result As HttpWebResponse = Request.GetResponse()Note: For the sake of simplicity I use the synchronous GetResponse() method, depending on the timeout you set, the UI may freeze if you didn't. Now once that is done we have to get the file name using the HttpWebResponse object. We wll do the following:
Code
Dim Filename As String = Result.ResponseUri.LocalPath
Filename = Filename.Substring(Filename.LastIndexOf("/") + 1)
MessageBox.Show(Filename)What we are doing here is declaring a string called Filename and we assign that the LocalPath property. We then change filename to the substring after the last index of "/", this is because the LocalPath gives us the folder the file is inside on the remote server.
Now our code looks like this:
Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Target As Uri
If Uri.TryCreate(TextBox1.Text, UriKind.Absolute, Target) Then
Dim Request As HttpWebRequest = HttpWebRequest.Create(Target)
Request.Method = "GET"
Request.Timeout = 120000 '2 minutes timeout
Dim Result As HttpWebResponse = Request.GetResponse() 'This can be made asynchronous using the .BeginGetResponse method
Dim Filename As String = Result.ResponseUri.LocalPath
Filename = Filename.Substring(Filename.LastIndexOf("/") + 1)
MessageBox.Show(Filename)
Else
MessageBox.Show("The inputted link is invalid...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End SubThe Result (Click to View)
If you would like to download my VS Solution, you may do so here
I hope you all enjoy 🎉

![[Image: 9b7270ff0c98159120083967db7b532e.png]](https://gyazo.com/9b7270ff0c98159120083967db7b532e.png)
