Click here to Skip to main content
15,307,555 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:

Guys i am creating a download manager in window form in vb.

For downloading i am having following code:
Dim webclient As New Net.WebClient()

       Dim URL As String
       Dim filename As String
       Dim filereader As StreamReader
       Dim line As String
       filereader = New StreamReader(OpenFileDialog1.FileName)
       TextBox1.Text = filereader.ReadLine()
       For i As Integer = 0 To nooflines
           URL = filereader.ReadLine()
           filename = "F:\downloaded file" & i
           line = filereader.ReadLine()
           Do While filereader.Peek() >= 0
               nooflines = nooflines + 1
               line = filereader.ReadLine()
           webclient.DownloadFile(URL, filename)


But in (webclient.DownloadFile(URL, filename)) this line i am having an error which says -

"The remote server returned an error: (401) Unauthorized."

It is working on another pc but in my laptop I am facing this error.
Updated 16-May-13 4:42am
Shawn19801980 16-May-13 10:47am
Make sure you have proper access to this file from a different location, you may not be authorized to use the file from your current location or you may not have access at all, check on your permissions. Remote access maybe denied for you.
shashank 1068 16-May-13 10:49am
how should i do this??

1 solution

Hello, Shashank,

Generally error message 401 means you need to log on (enter a valid user ID and password) somewhere first. If you have just entered these and then immediately see a 401 error, it means that one or both of your user ID and password were invalid for whatever reason (entered incorrectly, user ID suspended etc.).

What you really need to do is to supply Credentials to the WebClient something like shown below.
WebClient client = new WebClient();
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("username", "password");

If your app is running behind a authenticating proxy then you may want to supply the proxy server with credentials as shown below
WebClient client = new WebClient();
WebProxy wp = new WebProxy("PROXY_ID_OR_HOSTNAME", PROXY_PORT);
wp.Credentials = new NetworkCredential("NETWORK USER ID", "PASSWORD", "DOMAIN");
client.Proxy = wp;
client.DownloadFile("", "F:\\changfs.png");

shashank 1068 16-May-13 11:24am
do i really need to create username and password??
Prasad Khandekar 16-May-13 11:33am
replace those values with actual user id & password required to connect to the server.
Best way to test is to open the browser and type in the file url. The browser should popup a login dialog. Whatever credentials you supply here for downloading the file, exactly same will have to be supplied to the WebClient.
shashank 1068 16-May-13 11:36am
but as you are saying when i open the URL it directly display the image..
i am sending you that one URL
this is one of the URL which i am trying to download
Prasad Khandekar 16-May-13 12:06pm
I have tried with that URL and is working on my side. However I was getting 407 error as I was trying to download from behind the authenticating proxy. I have updated the solution to reflect this case. Hope it helps you to resolve the problem.
shashank 1068 16-May-13 12:09pm
ohkk so for this i have to declare my username n psswrd?
Prasad Khandekar 16-May-13 12:11pm
You don't have to put that information here. It's of no use for testing purpose. It's good in your network only. Please do not disclose it here.
shashank 1068 16-May-13 12:20pm
yup ohk ohk
shashank 1068 16-May-13 12:21pm
but i mean i have to declare my usrname psswrd in coding??
Prasad Khandekar 17-May-13 1:32am
Not necessarily. You can provide a configuration/settings screen and ask the end user to fill in these details.
shashank 1068 17-May-13 2:53am
ohkk thnx
n do u know how to give image name while downloading??
like the above code is for download the file..and when it download the images it gives the name like 0,1,2,3,4,5,6,7,8
i want to give image name as the last part of the URL
like if i have ""
so i want that when it download this file then the name of the file should be CHANGFS.PNG
do u know how to do this?
Prasad Khandekar 17-May-13 3:27am
Just extract the name from URL. URL.Substring(URL.LastIndexOf("/") + 1).

shashank 1068 17-May-13 3:28am
ohkk thnx alot :)
shashank 1068 20-May-13 2:52am
hi..sry to disturb u again but i need another help
do u know hoe to save file when checkbox is clicked??
actually in download manager i want to save the save with checkbox
so do u know how to save files with checkbox?
Prasad Khandekar 20-May-13 9:50am
How about developing a worker thread for file download and launch the same in OnCheckedChanged handler. You can find many examples on this site.
shashank 1068 20-May-13 14:20pm
heyy sry to dstrb u again.. but do you know how to use progress bar with percentage in webclient..which display its speed and percentage??
Prasad Khandekar 21-May-13 0:01am
Have a look at

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900