Mario
Mario

Reputation: 1488

Downloading a file using VB.net is incomplete

I searched everywhere but without benefit.

I'm using this code to download a file :

Imports System.IO
Imports System.Security.AccessControl
Imports System.Net
Imports System.ComponentModel

Public Class Step4

    Function GetUserName() As String
        If TypeOf My.User.CurrentPrincipal Is 
          System.Security.Principal.WindowsPrincipal Then
            ' The application is using Windows authentication.
            ' The name format is DOMAIN\USERNAME.
            Dim parts() As String = Split(My.User.Name, "\")
            Dim username As String = parts(1)
            Return username
        Else
            ' The application is using custom authentication.
            Return My.User.Name
        End If
    End Function

    Private Sub Step4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Code of Step3
        Dim URL As String = "http://skinsserver.exampleserver.com/skins.zip"
        Dim filename As String = "C:\Users\" + GetUserName() + "\AppData\Roaming\Applicationsettings\skins.zip"
        Using wc As New WebClient
            wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)")
            wc.DownloadFile(URL, filename)
        End Using
    End Sub
End Class

I tried everything but only 800 byte of the file is being downloaded while it is 22 KB.

Downloading via browser is fine.

Please help!

Thanks!

Upvotes: 0

Views: 3283

Answers (3)

Mario
Mario

Reputation: 1488

The problem was in my server, I cannot download the file from this server. Sorry @Youssef13 - even using a cookie didn't work.

Here is the solution: http://vbcity.com/blogs/xtab/archive/2016/04/13/how-to-upload-and-download-files-with-ftp-from-a-vb-net-application.aspx

I used FTP to download the file.

Upvotes: 0

Youssef13
Youssef13

Reputation: 4954

The URL you're trying to download from requires a cookie.

Use this:

wc.Headers.Add("Cookie", "__test=3c020c18923605cf39e4292d69038f3a")

Upvotes: 1

Gabi Tintarescu
Gabi Tintarescu

Reputation: 5

You could try the following:

My.Computer.Network.DownloadFile(
"http://skinsserver.exampleserver.com/skins.zip",
"path_where_you_want_to_download")

see https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/how-to-download-a-file

Upvotes: 0

Related Questions