0

I'm using a function to check if an external url exists. Here's the code with the status messages removed for clarity.

    public static bool VerifyUrl(string url)
    {
        url.ThrowNullOrEmpty("url");

        if (!(url.StartsWith("http://") || url.StartsWith("https://")))
            return false;

        var uri = new Uri(url);

        var webRequest = HttpWebRequest.Create(uri);
        webRequest.Timeout = 5000;
        webRequest.Method = "HEAD";

        HttpWebResponse webResponse;
        try
        {
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            webResponse.Close();
        }
        catch (WebException)
        {
            return false;
        }

        if (string.Compare(uri.Host, webResponse.ResponseUri.Host, true) != 0)
        {
            string responseUri = webResponse.ResponseUri.ToString().ToLower();

            if (responseUri.IndexOf("error") > -1 || responseUri.IndexOf("404.") > -1 || responseUri.IndexOf("500.") > -1)
                return false;
        }

        return true;
    }

I've run a test over some external urls and found that about 20 out of 100 are coming back as errors. If i add a user agent the errors are around 14%.

The errors coming back are "forbidden", although this can be resolved for 6% using a user agent, "service unavialable", "method not allowed", "not implemented" or "connection closed".

Is there anything I can do to my code to ensure more, preferrably all give a valid response to their existance?

Altermatively, code that can be purchased to do this more effectively.

UPDATE - 14th Nov 12 ----------------------------------------------------------------------

After following advice from previous respondants, I'm now in a situation where I have a single domain that returns Service Unavailable (503). The example I have is www.marksandspencer.com.

When I use this httpsniffer web-sniffer.net as opposed to the one recommended in this thread, it works, returning the data using a webrequest.GET, however I can't work out what I need to do, to make it work in my code.

4

1 に答える 1

0

最終的に、bieng がすべての URL を例外なく検証できるようになりました。

まず、ダヴィオスのアドバイスを受けました。一部のドメインは Request.HEAD でエラーを返すため、特定のシナリオでの再試行を含めました。これにより、2 番目のリクエスト用に新しい Request.GET が作成されました。

第二に、アマゾンのシナリオ。Amazon は、自身のサイトに対して断続的に 503 エラーを返し、Amazon フレームワークでホストされているサイトに対して永続的な 503 エラーを返していました。

掘り下げた後、次の行を Request に追加すると、両方が解決されることがわかりました。Firefox が使用する Accept 文字列です。

var request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
于 2012-11-16T10:53:15.573 に答える