首页 > 解决方案 > 将 Sharepoint 中的链接与 C# 一起使用时出错((401)未授权)

问题描述

当我上传图片时,sharepoint 会给出与该图片对应的链接。

我正在开发一个分析图像的 C# 项目,并希望使用 SHarepoint 的图像链接。

但是在执行从 WebClient 的 url() 加载图像的函数时,我被错误阻止了。

错误名称:“远程服务器返回错误(401)未授权”

这是我的 Sharepoint 中的图像显示链接(我正在扫描的链接):https ://ibb.co/g9jYHKC

这是我使用的代码 webclient():

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

期待早日收到大家的来信,谢谢

标签: c#sharepointwebclient

解决方案


HTTP 401表示未经授权,这意味着您未对您发出请求的资源进行身份验证。您需要通过服务器进行身份验证才能接受您的请求。

使用WebClient类,您可以通过Credentials类成员执行此操作:

//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");

很可能,Uri凭证缓存中使用的 URI 将与.DownloadData签名中使用的 URI 类似。


推荐阅读