检查URL是否工作或不使用 [英] Check for URL is working or not using

查看:139
本文介绍了检查URL是否工作或不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string url = "www.google.com";

public bool UrlIsValid(string url)
{
    bool br = false;
    try
    {
                    IPHostEntry ipHost =  Dns.GetHostEntry(url);
                                     br = true;
    }
    catch (SocketException)
    {
        br = false;
    }
    return br;
}



以上程序将输出正确的,但是当我改变字符串

Above program will output true but when I change string to

string url = "https://www.google.com";



我得到的输出为

我怎样才能得到输出为我的第二个案例?

How can I get output for my second case?

推荐答案

您可以尝试使用URI类来解析URL字符串。

You could try to use the Uri class to parse the url string.

public bool UrlIsValid(string url) {
   return UrlIsValid(new Uri(url));
}


public bool UrlIsValid(Uri url)
{
    bool br = false;
    try
    {
         IPHostEntry ipHost =  Dns.GetHostEntry(url.DnsSafeHost);
         br = true;
    }
    catch (SocketException)
    {
        br = false;
    }
    return br;
}

这篇关于检查URL是否工作或不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆