Edward Ha
Edward Ha

Reputation: 29

Error when use link from Sharepoint with C# ( (401) unauthorized )

When I upload an image, the sharepoint will give a link corresponding to that image.

I am working on a C # project that analyzes images, and want to use SHarepoint's image link.

But when executing the function to load the image from WebClient's url (), I was blocked by an error.

Error name: "the remote server returned an error (401) unauthorized"

this is image show link in my Sharepoint (the link that I am scanning): https://ibb.co/g9jYHKC

And this is code webclient() I used:

var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://pmssd78/Animal/birddd.jpg"); //link copy from sharepoint like image show

Looking forward to hearing from everybody soon, thanks

Upvotes: 0

Views: 1012

Answers (1)

Timothy G.
Timothy G.

Reputation: 9075

HTTP 401 means unauthorized, which means you aren't authenticated with the resource that you are making the request to. You need to authenticate with the server for it to accept your request.

With the WebClient class, you can do this via the Credentials class member:

//Create a Credential Cache - you'll want this to be defined somewhere appropriate, 
//maybe at a global level so other parts of your application can access it
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://yourSharepointUrl.com"), "Basic", new NetworkCredential("yourUserName", "yourSecuredPassword"));

//Create WebClient, set credentials, perform download
WebClient webClient = new WebClient();
webClient.Credentials = credentialCache.GetCredential(new Uri("http://yourSharepoint.com"), 
"Basic");
byte[] imageBytes = webClient.DownloadData("http://pmssd78/Animal/birddd.jpg");

Likely, the Uri used in the credential cache is going to be something along the lines of your URI being used in the .DownloadData signature.

Upvotes: 0

Related Questions