By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

WebClient.UploadData Authentication issue

P: 6
Hi All,
I try to use WebClient.UploadData to upload a photo with the size less than 1MB to my local IIS.

I setup my IIS by adding a new virtual directory with the write access to the photo (Please refer to the figure below for better understanding).



After that, I edit the photo's directory security by clearing up the anonymous access checkbox and check/tick the Integrated Windows authentication.(Please refer to the figure below)



At the end I write the following code to upload the photo. Assuming that my computer login is compaq, password is 1234abcd without join domain and proxy.

Expand|Select|Wrap|Line Numbers
  1. using (WebClient Client = new WebClient())
  2. {
  3.   using(FileStream fs = File.OpenRead("C:\abc.jpg"))
  4.   {
  5.      byte[] data = new byte[fs.Length];
  6.      fs.Read(data, 0, data.Length);
  7.      CredentialCache cache = new CredentialCache();
  8.      Uri UploadPhotoDircectory = new Uri("http://localhost/FFQWS/Photo/");
  9.      string AuthType = "Negotiate";
  10.      cache.Add(UploadPhotoDircectory, AuthType, new System.Net.NetworkCredential("compaq", "1234abcd"));
  11.      Client.Credentials = cache;
  12.      try
  13.      {
  14.         Client.UploadData("http://localhost/FFQWS/Photo/abc.jpg", "PUT", data);
  15.      }
  16.      catch(Exception ex)
  17.      {
  18.         MessageBox.Show(ex.Message);
  19.      }
  20. }
Alas, after UploadData I get the following exception.
The remote server returned an error:(401)Unauthorized
I already searched through the internet and comfirmed that I did insert the valid windows login. Hope that someone can help me to solve the issue. Your help is really very appreciated. Thank you very much. ^^
Feb 23 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.