4 Replies - 2885 Views - Last Post: 03 August 2011 - 10:29 AM Rate Topic: -----

#1 yyc2001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 20-February 09

Getting errors when I tried to download files using credentials

Posted 03 August 2011 - 09:40 AM

I'm quite new at C#. And one of my task is to download or read a xml file from the web and extract the information out.

I'm trying to download a xml file from a website that need to enter username, password, and domain name. I tried to download the file with the correct authentication but I keep getting errors. I'm not too sure where I did wrong. I would appreciate if you can give me a guidance on how to solve the problem. Thank you in advance.

The below is part of my code:
....
string downloadFile = @"C:\Documents and Settings\YY\Desktop\TestFolder\PST.xml";
string temp = node.ChildNodes[i].Attributes[j].Value ;
string[] split = temp.Split('#'); 
Console.WriteLine(count + " http://awebsite/" + split[1]);

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password", "domain" );
wc.DownloadFile("http://awebsite/" + split[1], downloadFile );
....




The below is the errors I'm getting: (line 247 is the wc.DownloadFile..... part)

 
ERROR!: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at TestSharePoint.TestSharePoint.ReadIncidentReport(Lists listService) in C:\Documents and Settings\YY\my documents\visual studio 2010\Projects\TestSharePoint\TestSharePoint\Program.cs:line 247



Is This A Good Question/Topic? 0
  • +

Replies To: Getting errors when I tried to download files using credentials

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5467
  • View blog
  • Posts: 11,743
  • Joined: 02-June 10

Re: Getting errors when I tried to download files using credentials

Posted 03 August 2011 - 09:49 AM

A. This sounds like your homework assignment and we don't do your homework for you.

B. You weren't authorized to download the file. That's not your code. That's you not having the right permissions. So my guess is that you didn't provide the right credentials.

I assume on line 8 that you are not really sending the strings, "username" and "password" are you?

Though I can't imagine why you would replace a variable holding your password with the string "password" just for this post. So many you are.

This post has been edited by tlhIn`toq: 03 August 2011 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 yyc2001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 20-February 09

Re: Getting errors when I tried to download files using credentials

Posted 03 August 2011 - 10:03 AM

View PosttlhIn`toq, on 03 August 2011 - 09:49 AM, said:

A. This sounds like your homework assignment and we don't do your homework for you.

B. You weren't authorized to download the file. That's not your code. That's you not having the right permissions. So my guess is that you didn't provide the right credentials.

I assume on line 8 that you are not really sending the strings, "username" and "password" are you?

Though I can't imagine why you would replace a variable holding your password with the string "password" just for this post. So many you are.


No it's not my homework assignment. The reason I did not put the real website and the correct credentials is because I'm not suppose to display it. Sorry for the misunderstanding.
I have the right credential because I can get through the website with the same credential I have in my code.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Getting errors when I tried to download files using credentials

Posted 03 August 2011 - 10:07 AM

Does the website implement Basic HTTP authentication or forms-based?
Was This Post Helpful? 1
  • +
  • -

#5 yyc2001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 20-February 09

Re: Getting errors when I tried to download files using credentials

Posted 03 August 2011 - 10:29 AM

View PostJackOfAllTrades, on 03 August 2011 - 10:07 AM, said:

Does the website implement Basic HTTP authentication or forms-based?


Thanks for the help. I finally figure why I'm getting error. I need to include download.asp in the url I have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1