Determine if a URL is valid via the API
I need to be able to query SF to determine if a URL is valid.
For instance, if I receive a request for "~/home/this-part-is-invalid/", I need to be able to check the path against the pages stored in SF to determine if it will return a valid page, or a 404.
How do I do this?
Hi Dave,
You could use a HttpWebRequest to get the URL and check if it is valid or not. What kind of functionality are you trying to implement? Is this for a widget or for a specific page? If you let me know I can give you better direction on where to drop this and how to build it out but here's the basics of URL validation:
using System.Net;
///
/// Checks the file exists or not.
///
/// The URL of the remote file.
/// True : If the file exits, False if file not exists
private bool RemoteFileExists(string url)
try
//Creating the HttpWebRequest
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//Setting the Request method HEAD, you can also use GET too.
request.Method = "HEAD";
//Getting the Web Response.
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Returns TURE if the Status code == 200
return (response.StatusCode == HttpStatusCode.OK);
catch
//Any exception will returns false.
return false;