Linkchecker不打印任何破坏的网址

时间:2012-03-16 12:47:22

标签: c# asp.net

我在创建一个linkchecker时遇到了问题,我想在网上主要用于学习..

问题是我第一次将它作为一个控制台应用程序,它工作得很好(我有破坏的网址显示我调试控制台),现在我正在尝试将它发送到网络我遇到了麻烦.. < / p>

如何将其纳入文档?我此刻难过......

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);
}
}
}

希望你能理解我想要的成就,现在很难理解......

1 个答案:

答案 0 :(得分:1)

我认为您希望Response.Write()代替您的Debug.WriteLine()方法。或者,您可以在标记中创建TextArea对象并使用myTextArea.Text += "Some text";