C# 如何检查 URL 是否存在/有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:20:00  来源:igfitidea点击:

C# How can I check if a URL exists/is valid?

c#.neturl-validation

提问by Daniel Waltrip

I am making a simple program in visual c# 2005 that looks up a stock symbol on Yahoo! Finance, downloads the historical data, and then plots the price history for the specified ticker symbol.

我正在用 Visual c# 2005 制作一个简单的程序,用于在 Yahoo! 上查找股票代码!Finance,下载历史数据,然后绘制指定股票代码的价格历史。

I know the exact URL that I need to acquire the data, and if the user inputs an existing ticker symbol (or at least one with data on Yahoo! Finance) it works perfectly fine. However, I have a run-time error if the user makes up a ticker symbol, as the program tries to pull data from a non-existent web page.

我知道我需要获取数据的确切 URL,如果用户输入一个现有的股票代码(或至少一个包含 Yahoo! Finance 数据的代码),它工作得非常好。但是,如果用户创建一个股票代码,我会遇到运行时错误,因为程序试图从不存在的网页中提取数据。

I am using the WebClient class, and using the DownloadString function. I looked through all the other member functions of the WebClient class, but didn't see anything I could use to test a URL.

我正在使用 WebClient 类,并使用 DownloadString 函数。我查看了 WebClient 类的所有其他成员函数,但没有看到任何可以用来测试 URL 的内容。

How can I do this?

我怎样才能做到这一点?

采纳答案by Marc Gravell

You could issue a "HEAD"request rather than a "GET"?

您可以发出“HEAD”请求而不是“GET”吗?

(edit) - lol! Looks like I've done this before!; changed to wiki to avoid accusations of rep-garnering. So to test a URL without the cost of downloading the content:

(编辑) - 大声笑!看起来我以前做过这个!更改为 wiki 以避免被指控收集代表。因此,要在不下载内容的情况下测试 URL:

// using MyClient from linked post
using(var client = new MyClient()) {
    client.HeadOnly = true;
    // fine, no content downloaded
    string s1 = client.DownloadString("http://google.com");
    // throws 404
    string s2 = client.DownloadString("http://google.com/silly");
}

You would try/catcharound the DownloadStringto check for errors; no error? It exists...

你会try/catch周围DownloadString来检查错误;没有错误?它存在...



With C# 2.0 (VS2005):

使用 C# 2.0 (VS2005):

private bool headOnly;
public bool HeadOnly {
    get {return headOnly;}
    set {headOnly = value;}
}

and

using(WebClient client = new MyClient())
{
    // code as before
}

回答by David Taylor

Web servers respond with a HTTP status code indicating the outcome of the request e.g. 200 (sometimes 202) means success, 404 - not found etc (see here). Assuming the server address part of the URL is correct and you are not getting a socket timeout, the exception is most likely telling you the HTTP status code was other than 200. I would suggest checking the class of the exception and seeing if the exception carries the HTTP status code.

Web 服务器以 HTTP 状态代码响应,指示请求的结果,例如 200(有时为 202)表示成功,404 - 未找到等(请参阅此处)。假设 URL 的服务器地址部分是正确的并且您没有收到套接字超时,则异常很可能告诉您 HTTP 状态代码不是 200。我建议检查异常的类并查看异常是否携带HTTP 状态代码。

IIRC - The call in question throws a WebException or a descendant. Check the class name to see which one and wrap the call in a try block to trap the condition.

IIRC - 有问题的调用引发 WebException 或后代。检查类名以查看是哪一个并将调用包装在 try 块中以捕获条件。

回答by Calendar Software

If I understand your question correctly, you could use a small method like this to give you the results of your URL test:

如果我正确理解您的问题,您可以使用这样的小方法来为您提供 URL 测试的结果:

WebRequest webRequest = WebRequest.Create(url);  
WebResponse webResponse;
try 
{
  webResponse = webRequest.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
  return 0;
} 
return 1;

You could wrap the above code in a method and use it to perform validation. I hope this answers the question you were asking.

您可以将上述代码包装在一个方法中并使用它来执行验证。我希望这能回答你提出的问题。

回答by BigJoe714

Here is another implementation of this solution:

这是此解决方案的另一个实现:

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 TRUE if the Status code == 200
        response.Close();
        return (response.StatusCode == HttpStatusCode.OK);
    }
    catch
    {
        //Any exception will returns false.
        return false;
    }
}

From: http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/

来自:http: //www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/

回答by abobjects.com

This solution seems easy to follow:

这个解决方案似乎很容易遵循:

public static bool isValidURL(string url) {
    WebRequest webRequest = WebRequest.Create(url);
    WebResponse webResponse;
    try
    {
        webResponse = webRequest.GetResponse();
    }
    catch //If exception thrown then couldn't get response from address
    {
        return false ;
    }
    return true ;
}

回答by jsmith

These solutions are pretty good, but they are forgetting that there may be other status codes than 200 OK. This is a solution that I've used on production environments for status monitoring and such.

这些解决方案都不错,但是他们忘记了可能还有其他状态码而不是 200 OK。这是我在生产环境中用于状态监控等的解决方案。

If there is a url redirect or some other condition on the target page, the return will be true using this method. Also, GetResponse() will throw an exception and hence you will not get a StatusCode for it. You need to trap the exception and check for a ProtocolError.

如果目标页面上存在 url 重定向或其他一些条件,则使用此方法返回 true。此外,GetResponse() 将引发异常,因此您不会获得它的 StatusCode。您需要捕获异常并检查 ProtocolError。

Any 400 or 500 status code will return false. All others return true. This code is easily modified to suit your needs for specific status codes.

任何 400 或 500 状态代码都将返回 false。所有其他人返回true。可以轻松修改此代码以满足您对特定状态代码的需求。

/// <summary>
/// This method will check a url to see that it does not return server or protocol errors
/// </summary>
/// <param name="url">The path to check</param>
/// <returns></returns>
public bool UrlIsValid(string url)
{
    try
    {
        HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
        request.Timeout = 5000; //set the timeout to 5 seconds to keep the user from waiting too long for the page to load
        request.Method = "HEAD"; //Get only the header information -- no need to download any content

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            int statusCode = (int)response.StatusCode;
            if (statusCode >= 100 && statusCode < 400) //Good requests
            {
                return true;
            }
            else if (statusCode >= 500 && statusCode <= 510) //Server Errors
            {
                //log.Warn(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
                Debug.WriteLine(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
                return false;
            }
        }
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
        {
            return false;
        }
        else
        {
            log.Warn(String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url), ex);
        }
    }
    catch (Exception ex)
    {
        log.Error(String.Format("Could not test url {0}.", url), ex);
    }
    return false;
}

回答by tsingroo

i have a more simple way to determine weather a url is valid.

我有一种更简单的方法来确定 url 的天气是否有效。

if (Uri.IsWellFormedUriString(uriString, UriKind.RelativeOrAbsolute))
{
   //...
}

回答by Zain Ali

Here is another option

这是另一种选择

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

回答by user3154431

Following on from the examples already given, I'd say, it's best practice to also wrap the response in a using like this

继已经给出的示例之后,我想说,最好的做法是也将响应包装在这样的使用中

    public bool IsValidUrl(string url)
    {
         try
         {
             var request = WebRequest.Create(url);
             request.Timeout = 5000;
             request.Method = "HEAD";

             using (var response = (HttpWebResponse)request.GetResponse())
             {
                response.Close();
                return response.StatusCode == HttpStatusCode.OK;
            }
        }
        catch (Exception exception)
        { 
            return false;
        }
   }

回答by user6909992

Try this (Make sure you use System.Net):

试试这个(确保你使用 System.Net):

public bool checkWebsite(string URL) {
   try {
      WebClient wc = new WebClient();
      string HTMLSource = wc.DownloadString(URL);
      return true;
   }
   catch (Exception) {
      return false;
   }
}

When the checkWebsite() function gets called, it tries to get the source code of the URL passed into it. If it gets the source code, it returns true. If not, it returns false.

当 checkWebsite() 函数被调用时,它会尝试获取传递给它的 URL 的源代码。如果它获取源代码,则返回 true。如果不是,则返回 false。

Code Example:

代码示例:

//The checkWebsite command will return true:
bool websiteExists = this.checkWebsite("https://www.google.com");

//The checkWebsite command will return false:
bool websiteExists = this.checkWebsite("https://www.thisisnotarealwebsite.com/fakepage.html");