0

I found this code from here it works for uri like "https://google.com" but it doesn't work for something like "https://172.61.58.168/Account/Login/". I actually don't know why. I have an App using that URI that has an API that I need to call. I can successfully do an api-call and I just decided to make a validation first if the base uri exist and is valid.

Help. Thanks.

code:

bool isHttpValid = false;
try
{
    //Creating the HttpWebRequest
    HttpWebRequest request = WebRequest.Create("https://172.16.85.186/Account/Login/") 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 TRUE if the Status code == 200
    response.Close();
    isHttpValid = true;
}
catch
{
    //Any exception will returns false.
    isHttpValid = false;
}
Joseph Reyes
  • 69
  • 1
  • 3
  • 10
  • do a debug on the response code. – BugFinder May 22 '19 at 07:15
  • System.Net.WebException: The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. – Joseph Reyes May 22 '19 at 07:19
  • Well, you dont have any code to accept a certificate... that makes perfect sense – BugFinder May 22 '19 at 07:20
  • 1
    @JosephReyes Edit your question and put details there, not in comments. – Gaurang Dave May 22 '19 at 07:20
  • @BugFinder What code should I add? – Joseph Reyes May 22 '19 at 07:34
  • @JosephReyes The exception you posted there previously seems interesting. Post it with the question. – Gonzo345 May 22 '19 at 07:58

1 Answers1

0

First of all you should use HttpClient instead of WebRequest, then you are checking that a HTTP request is valid, however your using a HTTPS URL.

If you want a method that check both HTTP and HTTPS URLs, you have to think how are you going to manage HTTPS certificates, due to that exception:

System.Net.WebException: The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

Maybe you want to add that custom certificate as secure, or you want to remove the security policy that checks the SSL certificates (I don't recommend it), following that other question.

I hope this can help you.

ganchito55
  • 3,559
  • 4
  • 25
  • 46