There are some urls which are only accessible once the VPN is connected. Ex: http://jira.xxx.com. for a company say 'xxx'.
I want to know if there is any way in C# that could help me to check if I have an access to this url when VPN is connected. The core purpose here is to return an error if the VPN is not connected asking the user to try again.
This is what I tried by making use of HttpWebRequest and HttpWebResponse, but I am recieving the response as null even when the VPN is connected. (Please know I was able to access the url manually but the response is seen as null in the code, not sure why)
public static bool IsUrlAccessible(string url)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
response.Close();
int statusCode = (int)response.StatusCode;
Console.WriteLine("Status code is {0}", statusCode);
if (statusCode >= 100 && statusCode < 400)
{
return true;
}
}
}
catch (WebException ex)
{
Console.WriteLine("VPN is not connected."), ex);
}
return false;
}
Since I was accessing a HTTPS page I had to set the Service Point Security Protocol to Tls12
. As we can set multiple protocols. I adopted the most preferable case.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;