2012-03-16 76 views
0

我在创建和linkchecker的问题,我想在网上有它主要用于学习..和linkchecker犯规打印任何损坏的网址

的问题是,我首先它作为有点儿工作控制台应用程序好吧(我得到了破坏的URL显示我调试控制台),现在,我正在试图让它到网上我遇到了麻烦..

我该如何去获取这个文件?我有点此刻难倒..

public partial class Default2 : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 


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 

    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 
    { 
     string cl = (String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url)); 
     // Debug.WriteLine(cl, Convert.ToString(url)); 

     return false; 
    } 
} 
catch (WebException ex) 
{ 
    if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors 
    { 
     return false; 
    } 
    else 
    { 
     string cl = String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url); 
     /// Debug.WriteLine(cl, Convert.ToString(ex)); 

    } 
} 
catch (Exception ex) 
{ 
    object cl = String.Format("Could not test url {0}.", url); 
    Debug.WriteLine(cl, Convert.ToString(ex)); 
} 
return false; 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
WebClient wc = new WebClient(); 

string checker = wc.DownloadString("http://administration.utbildningssidan.se/linkcheck.aspx"); 

while (checker.Contains("<a href=")) 
{ 
    int checkstart = checker.IndexOf("<a href=") + 8; 
    int checkstop = checker.IndexOf(">", checkstart); 
    string validator = checker.Substring(checkstart, checkstop - checkstart); 

    // perform the check 
    if (!UrlIsValid(validator)) { Debug.WriteLine(validator); } 

    checker = checker.Substring(checkstop + 1); 
} 
} 
} 

希望你明白我要完成的,现在有一个很难决策意识..

+0

你是怎么调用* UrlIsValid?我没有在帖子包括它 – 2012-03-16 12:59:54

+0

没有注意到,这里就是它的所谓的(它位于“的button1_Click”的底部: 如果(UrlIsValid(验证)== FALSE){ 的Debug.WriteLine(验证); } checker = checker.Substring(checkstop + 1); – Jesper 2012-03-19 08:21:05

回答

1

我觉得你代替你Debug.WriteLine()方法要Response.Write() 。或者,你可以在你的标记中创建一个TextArea对象,并使用myTextArea.Text += "Some text";