Sub DownloadFile()
Dim wc As New WebClient
Try
Dim durl As String = "https://onedrive.live.com/download?cid=9B804CF9A004FFF3&resid=9B804CF9A004FFF3%212166&authkey=ABv40n0yI5bLOAo"""
'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(durl, Application.StartupPath & "\Temp.rar")
wc.DownloadFileAsync(New Uri(durl), "test.rar")
Catch ex As Exception
SendToLogFile("Download " & ex.Message)
End Try
End Sub
Problem:
with wc.Headers.Add
method downloaded file size was 78 kb, without headers download file size is 28 Kb. actual file size is 110kb or more . I tried in another pc assuming network/firewall issue but the result is same and incomplete.
What I Tried :
I tried the downloadfileasync
method, the downloadfile
method, and the My.Computer.Network.DownloadFile
method, but they all resulted in a partial download. When I paste the url into Chrome or another browser, it works perfectly.
I need this code to work on all Windows operating system versions
To do that you can use several techniques. Here a pair of those:
Try
'Using HttpClient
Using wc As HttpClient = New HttpClient()
Dim bytes() As Byte = wc.GetByteArrayAsync("https://onedrive.live.com/download?cid=9B804CF9A004FFF3&resid=9B804CF9A004FFF3%212166&authkey=ABv40n0yI5bLOAo").Result
Using sw As New MemoryStream(bytes)
Using sr As Stream = File.Create("test.rar")
sw.CopyTo(sr)
End Using
End Using
End Using
'Using WebClient
Using wc As WebClient = New WebClient()
Dim bytes() As Byte = wc.DownloadData("https://onedrive.live.com/download?cid=9B804CF9A004FFF3&resid=9B804CF9A004FFF3%212166&authkey=ABv40n0yI5bLOAo")
Using sw As New MemoryStream(bytes)
Using sr As Stream = File.Create("test1.rar")
sw.CopyTo(sr)
End Using
End Using
End Using
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try