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

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

For downloading i am having following code:
VB
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()
           MsgBox(URL)
           filename = "F:\downloaded file" & i
           line = filereader.ReadLine()
           Do While filereader.Peek() >= 0
               nooflines = nooflines + 1
               line = filereader.ReadLine()
           Loop
           webclient.DownloadFile(URL, filename)


       Next

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.
Posted
Updated 16-May-13 4:42am
v2
Comments
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.
C#
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
C#
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("http://www.monsoondata.org/wx/changfs.png", "F:\\changfs.png");

Regards,
 
Share this answer
 
v2
Comments
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
http://www.monsoondata.org/wx/changfs.png
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?

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