I am validating a regular URL as follows:
private static bool IsUrlAvailable(string url)
{
if ((string.IsNullOrEmpty(url.Trim()) == true) ||
(url.Trim().ToLower().Equals("http://")) ||
(url.Trim().ToLower().Equals("https://")))
{
return false;
}
if (!url.ToLower().StartsWith("http://") && !url.ToLower().StartsWith("https://"))
{
url = "http://" + url;
}
try
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.Timeout = 15000;
req.Method = "HEAD";
using (var rsp = (HttpWebResponse)req.GetResponse())
{
if (rsp.StatusCode == HttpStatusCode.OK)
{
return true;
}
}
}
catch (Exception ex)
{
// Eat it because all we want to do is return false
}
// Otherwise
return false;
}
But since I am using a WebRequest.Create, authenticated SharePoint urls on the intranet are failing validation because of permission denied (404) error. Now I know we can validate it using SPSite.Exists or OpenWeb but these are only available in microsoft.sharepoint.dll and I was wondering if there is a way of doing this without using this DLL?
404 is not permission denied… but file-not-found. So you may indeed hit non-existing Url.
You need to pass credentials with your request
Side note: please do not “eat all exceptions” – there is very small set of exceptions that are interesting in case of making WebRequests which you can find in article about HttpWebRequest.GetResponse (you likley should only handle WebException).