400使用HttpWebRequest和HttpWebResponse Why检查网站是否有效时出错



在此处使用以下代码来确定url是否有效:

public bool UrlIsValid(string url)
{
    if(!url.ToLower().StartsWith("http://") && !url.ToLower().StartsWith("https://"))
    {
        url = "http://" + 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
        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));
            return false;
        }
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
        {
            log.Warn(String.Format("400 Error logged: {0}", url));
            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;
}

问题是,它似乎返回false,带有以下url:https://www.paypal.com,甚至http://www.paypal.com在日志中给我一个400错误。为什么?有什么办法绕过这个吗?

您的代码适用于其他站点。

PayPal不得响应HEAD请求。

返回的System.Net.WebException显示"未处理"

最简单的方法就是使用GET。

相关内容

  • 没有找到相关文章

最新更新