2014-04-17 4 views
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를 반환하는 것입니다. 왜 그런가요? 이 문제를 해결할 여지가 있습니까?

답변

0

다른 사이트에서도 괜찮습니다.

HEAD 요청에 PayPal이 응답하지 않아야합니다. System.Net.WebException 반환

이 약 '처리되지 않은'

가장 쉬운 방법은 바로 GET을 사용하는 것입니다 말했다.

+0

'GET '을 사용하는 경우 많은 코드를 변경해야합니까, 아니면 단순히 코드에서'HEAD'에서'GET'으로 간단하게 전환 할 수 있습니까? –

+1

request.Method = "GET"해야합니다. –