0
: https://www.paypal.com
또는 로그에 나에게 400 오류를주는 http://www.paypal.com
로 :400 웹 사이트가 HttpWebRequest 및 HttpWebResponse와 함께 유효한지 확인하는 중 오류가 발생하는 이유는 무엇입니까? 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;
}
문제는 다음과 같은 URL로 false를 반환하는 것입니다. 왜 그런가요? 이 문제를 해결할 여지가 있습니까?
'GET '을 사용하는 경우 많은 코드를 변경해야합니까, 아니면 단순히 코드에서'HEAD'에서'GET'으로 간단하게 전환 할 수 있습니까? –
request.Method = "GET"해야합니다. –