0
votes

I get the 401 message when I try to get an image from a local SharePoint site. Tried a number of credentials settings but they all return the same 401 error.

var SharePointURL = "http://SharePointServer/Site/ImageFolder/image1.jpg";
System.Net.WebRequest req = System.Net.WebRequest.Create(SharePointURL);
req.Credentials = System.Net.CredentialCache.DefaultCredentials;
req.UseDefaultCredentials = true;              
System.Net.WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
stream.Close();

Any help would be appreciated.

1
401 errors are common with SharePoint. But 401.1, 401.2, 401.3 can have different signification. Take a look in the IIS logs to see what is exactly the http status code, and especially the substatus code (the substatus is not sent to the client, IIS logs are the only places where you can find this information).Steve B
Thanks, I had a look at the IIS log and I cant even find the server name that is used in my request?netDeveloperUK

1 Answers

0
votes

I don't know why this is happening, but I can tell you what you can do to find out for yourself:

Assuming you are running your code on a different machine to the one you are running SharePoint on, you need to have Fiddler installed on your machine. Get that to intercept the request from your code and the same request from your browser. You should be able to see a difference between the requests, which will be useful to guide you as to what the cause of the problem is. You can also use Fiddler to try changing parameters so you can see which headers cause problems by their presence or absence.

If your code makes exactly the same request that your browser does, you will get exactly the same response.

If you find yourself still baffled after all of that, did you know that SharePoint has its own Stack Exchange.