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
- using (WebClient Client = new WebClient())
- {
- using(FileStream fs = File.OpenRead("C:\abc.jpg"))
- {
- byte[] data = new byte[fs.Length];
- fs.Read(data, 0, data.Length);
- CredentialCache cache = new CredentialCache();
- Uri UploadPhotoDircectory = new Uri("http://localhost/FFQWS/Photo/");
- string AuthType = "Negotiate";
- cache.Add(UploadPhotoDircectory, AuthType, new System.Net.NetworkCredential("compaq", "1234abcd"));
- Client.Credentials = cache;
- try
- {
- Client.UploadData("http://localhost/FFQWS/Photo/abc.jpg", "PUT", data);
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
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. ^^