Question Strange behaviour with WebClient.DownloadFile

zeha

Member
Joined
Oct 30, 2012
Messages
5
Programming Experience
10+
I try to download some files from sourceforge.net, it works fine for most of the files, but I really don't find my mistake here:

VB.NET:
Public Class Form1
    Public WithEvents WebClient1 As New Net.WebClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim URLPart1$ = "http://downloads.sourceforge.net/portableapps/"
        Dim FileName1$ = "FirefoxPortableTest_17.0_Beta_3_English.paf.exe"
        WebClient1.DownloadFile(New Uri(URLPart1 & FileName1), "D:\" & FileName1)

        Dim FileName2$ = "FirefoxPortable_16.0.2_English.paf.exe"
        Try
            WebClient1.DownloadFile(New Uri(URLPart1 & FileName2), "D:\" & FileName2)
            'Works in Browser - w/o 404: http://downloads.sourceforge.net/portableapps/FirefoxPortable_16.0.2_English.paf.exe why it won't work here?
        Catch ex As Exception
            MsgBox(URLPart1 & FileName2 & vbNewLine & vbNewLine & ex.ToString)
        End Try
    End Sub
End Class

Hope someone here may help me with that.

I cannot understand why I can download it with browser but not with vb.net, and I also dont understand why it works for the first file...

Kind regards,
zeha
 
Looks like there's a failure in the redirect at sourceforge. This works ..

"http://sourceforge.net/projects/portableapps/files/Mozilla%20Firefox%2C%20Portable%20Ed./Mozilla%20Firefox%2C%20Portable%20Edition%2016.0.2/FirefoxPortable_16.0.2_English.paf.exe"
 
Looks like there's a failure in the redirect at sourceforge. This works ..

"http://sourceforge.net/projects/portableapps/files/Mozilla%20Firefox%2C%20Portable%20Ed./Mozilla%20Firefox%2C%20Portable%20Edition%2016.0.2/FirefoxPortable_16.0.2_English.paf.exe"

Not really ...

I only get about 400 kB of html code in the file, but not the executable I expect

Any Idea how, for example, Firefox could solve this problem? Cause with it I can download the file, so there must be a way from "http://downloads.sourceforge.net/portableapps/FirefoxPortable_16.0.2_English.paf.exe" to the executable on my disk
 
Not really ...

I only get about 400 kB of html code in the file, but not the executable I expect

Any Idea how, for example, Firefox could solve this problem? Cause with it I can download the file, so there must be a way from "http://downloads.sourceforge.net/portableapps/FirefoxPortable_16.0.2_English.paf.exe" to the executable on my disk

You mean apart from the fact that it's a fully fledged browser with the relevant script engines and so on whilst you're trying to download using a default Web Request? That's like asking why a mouse can't pull a donkey cart!
 
You mean apart from the fact that it's a fully fledged browser with the relevant script engines and so on whilst you're trying to download using a default Web Request? That's like asking why a mouse can't pull a donkey cart!
Yes ;-)

Maybe it is possible to talk to sourceforge/portableapps, if they do a mistake in redirecting, but I think before I can do so, I have to narrow down the problem quite more. But I cannot really find the redirecting error, even if your assumption sounds not bad and possible.

I'd hoped that it is possible to find a way to load this file with .net, and hoped to find this help here... I really don't know if it is possible, but I doubt that it is impossible ;-)

I even know a .nsi (NullSoft Scripting Language) script that should load this file (I will test that later this day, but I'm quite sure that this script works)... but my experience with NSIS seems to be nearly in existent ;-) ... so I don't really suss how it works there. Unfortunately this script is about 80 kB, it seems no good idea to post it here - maybe I can cropping definitely unimportant stuff, so it's readable here, if someone can do something with that.

But you're definitely right, maybe Web Request / WebClient isn't the right Class to get over it. But I don't know an other way... New ideas and suggestions are always welcome, that's why I'm here.



[Edit:]I've tried the nsi script yesterday, and obviously it loads the file, that I cannot load with .net (yet).

even the cropped script is quite large, so maybe one (long) line helps a little, that should make the download:
VB.NET:
inetc::get /CONNECTTIMEOUT 30 /USERAGENT "Wget/1.13.4" /NOCOOKIES /TRANSLATE $strTempMessage $strTransDownloadConnecting $strTransTimeSecond $strTransTimeMinute $strTransTimeHour $strTransTimePlural "%d$strTransTimeKB (%d%%) of %d$strTransTimeKB @ %d.%01d$strTransTimeKBs" " (%d %s%s $strTransTimeRemaining)" "$strCurrentAppDownloadPath$strCurrentAppDownloadFile" "$PLUGINSDIR\$strCurrentAppDownloadFile" /END
but I cannot solve all the parameters and don't understand which does what.

Source: Download PortableApps.com: Portable Software/USB from SourceForge.net \PortableApps.comUpdaterU.nsi
[/Edit]
 
Last edited:
I've tried to manipulate the user-agent as in the nsi script, but I'm not sure if it's working this way, cause the Key vanishes after .DownloadFile.

VB.NET:
Public Class Form1
    Public WithEvents WebClient1 As New Net.WebClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim URLPart1$ = "http://downloads.sourceforge.net/portableapps/"
        'Dim FileName1$ = "FirefoxPortableTest_17.0_Beta_3_English.paf.exe"
        Dim FileName2$ = "FirefoxPortable_16.0.2_English.paf.exe"
        'Dim FileName3$ = "http://sourceforge.net/projects/portableapps/files/Mozilla%20Firefox%2C%20Portable%20Ed./Mozilla%20Firefox%2C%20Portable%20Edition%2016.0.2/FirefoxPortable_16.0.2_English.paf.exe"
        Try
            Dim wck$() = WebClient1.Headers.AllKeys
            Dim type As Type = WebClient1.Headers.GetType()
            Dim flags As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
            Dim method As Reflection.MethodInfo = type.GetMethod("AddWithoutValidate", flags)
            method.Invoke(WebClient1.Headers, New String() {"user-agent", "Wget/1.13.4"})
            'New Key is set
            wck = WebClient1.Headers.AllKeys
            WebClient1.DownloadFile(New Uri(URLPart1 & FileName2), "D:\" & FileName2)
            'Works in Browser - w/o 404: http://downloads.sourceforge.net/portableapps/FirefoxPortable_16.0.2_English.paf.exe why it won't work here?
            'here the KEy is vanished
            wck = WebClient1.Headers.AllKeys
        Catch ex As Exception
            MsgBox(URLPart1 & FileName2 & vbNewLine & vbNewLine & ex.ToString)
        End Try
    End Sub
End Class

Also I'm not sure, if the user-agent is the single point of failure.
For the sake of completeness: it doesn't work with FileName3, too.

Any ideas are welcome :)
 
At the beginning again.

The user-agent does not has any effect.

I've tried a very simple .nsi:
VB.NET:
OutFile "FF-Downloader_16.0.2.exe"
AutoCloseWindow true

!define URL "http://downloads.sourceforge.net/portableapps/FirefoxPortable_16.0.2_English.paf.exe"
!define DEST "D:\FirefoxPortable_16.0.2_English.paf.exe"

Section "1"
    inetc::get "${URL}" "${DEST}" /END
SectionEnd
It doesn't do more than download the file from URL to DEST (without any options like user-agent), and here it does it's job, in vb.net it doesn't :confused:
 
Last edited:

Latest posts

Back
Top