How to pass credentials to httpwebrequest for accessing SharePoint Library

Tortoise picture Tortoise · Mar 4, 2011 · Viewed 134.5k times · Source

I'm trying to read files from a SharePoint document library using HttpWebRequest. In order to do that I have to pass some credentials. I'm using the below request:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/msexcel";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
request.Credentials = new NetworkCredential(UserName, PassWord);

Is this the correct way to pass credentials?

Answer

Mihai Brustur picture Mihai Brustur · Mar 14, 2011

You could also use:

request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;