¿Cómo comprobar si una cadena es una URL HTTP válida?


Hay Uri.IsWellFormedUriString y Uri.TryCreate métodos, pero que parecen volver true para rutas de archivo, etc.

¿Cómo puedo comprobar si una cadena es una URL HTTP válida (no necesariamente activa) para fines de validación de entrada?

Author: Ajay, 2011-09-28

6 answers

Pruebe esto para validar las URL HTTP (uriName es el URI que desea probar):

Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) 
    && uriResult.Scheme == Uri.UriSchemeHttp;

O, si desea aceptar las URL HTTP y HTTPS como válidas (según el comentario de J0e3gan):

Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) 
    && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
 338
Author: Arabela Paslaru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-12 21:58:12

Este método funciona bien tanto en http como en https. Solo una línea:)

if (Uri.IsWellFormedUriString("https://www.google.com", UriKind.Absolute))

MSDN: IsWellFormedUriString

 65
Author: Kishath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-03 19:28:01
    public static bool CheckURLValid(this string source)
    {
        Uri uriResult;
        return Uri.TryCreate(source, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
    }

Uso:

string url = "htts://adasd.xc.";
if(url.CheckUrlValid())
{
  //valid process
}

ACTUALIZACIÓN: (una sola línea de código) Gracias @GoClimbColorado

public static bool CheckURLValid(this string source) => Uri.TryCreate(source, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttps;

Uso:

string url = "htts://adasd.xc.";
if(url.CheckUrlValid())
{
  //valid process
}
 20
Author: Erçin Dedeoğlu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-25 07:40:42

Después de Uri.TryCreate puede comprobar Uri.Scheme para ver si HTTP(s).

 5
Author: Miserable Variable,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-02 09:37:12

Esto devolvería bool:

Uri.IsWellFormedUriString(a.GetAttribute("href"), UriKind.Absolute)
 1
Author: user3760031,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-20 12:02:17
Uri uri = null;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)
    return false;
else
    return true;

Aquí url está la cadena que tienes que probar.

 1
Author: Eranda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-02 09:43:09