Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Parsing file name from URL the proper way!

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:

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 timeout

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:

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 Sub

The Result (Click to View)

If you would like to download my VS Solution, you may do so here

I hope you all enjoy 🎉
(Dec 6, 2014 02:42 PM)SIM-1456245 Wrote:  Wow I must say, amazing tutorial bro. Nice concept and very nice explanation. Keep it up.

Thanks Electro! I have been so busy with school lately and working on Proton RAT so I haven't been able to contribute much :/
😁 Net.HttpWebRequest.Create(New Uri("Url", UriKind.Absolute)).GetResponse.ResponseUri.LocalPath
(Dec 6, 2014 02:56 PM)Only3 Wrote:  😁 Net.HttpWebRequest.Create(New Uri("Url", UriKind.Absolute)).GetResponse.ResponseUri.LocalPath

I like to explain every single line of code so that others that don't understand, will :)
🙂 its good tut i bet for 99% its new
(Dec 6, 2014 03:09 PM)Only3 Wrote:  🙂 its good tut i bet for 99% its new

Thanks :) -- One liners argh ahha xD


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by outlawNov 28, 2020 04:25 AM 12 367 Nov 28, 2020 04:25 AM
Last Post: Rmdy
[C] by lo0srMar 22, 2020 02:02 PM 3 129 Mar 22, 2020 02:02 PM
Last Post: HFDone
[C#] by DeadShot32Sep 15, 2019 02:36 AM 3 187 Sep 15, 2019 02:36 AM
Last Post: riu41776