问题
We developed a WebPart to read the content of the file in the document library in the SiteCollection. I used the following code to read the content.
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
string documenturl = siteurl+"/" + file.Url.ToString();
content = wc.DownloadData(documenturl);//documenturl is the file path of the document
But, i got the following error 401 unathorized exception
System.Net.WebException: The remote server returned an error:(401) Unauthorized. at System.Net.WebClient.DownloadDataInternal(Uri address,WebRequest& request) at System.Net.WebClient.DownloadData(Uri address)
For your information, i already tried to download document by SPFile openBinary method. But it only works when the document is small. Please refer the below site.
getting ComException while reading the document in SharePoint 2010
Thanks in advance.
回答1:
Probably the local loopback check. (Verify by trying to access your site using the same URL whilst using IE running on your SharePoint server)
Also - any reason why you're using web services to access local content rather than the SharePoint object model (Microsoft.SharePoint.dll)?
来源:https://stackoverflow.com/questions/7554659/401-unauthorized-exception-while-reading-data-from-the-document-library-sharepoi