Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linkchecker不会打印任何损坏的URL_C#_Asp.net - Fatal编程技术网

C# Linkchecker不会打印任何损坏的URL

C# Linkchecker不会打印任何损坏的URL,c#,asp.net,C#,Asp.net,我在创建链接检查器时遇到问题,我希望它主要用于在线学习 问题是,我第一次把它作为一个控制台应用程序,它工作得很好(我得到了坏的URL来显示我调试控制台),现在我试图把它放到web上,我遇到了麻烦 我该如何将其写入文档?我现在有点不知所措 public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public bool UrlIsV

我在创建链接检查器时遇到问题,我希望它主要用于在线学习

问题是,我第一次把它作为一个控制台应用程序,它工作得很好(我得到了坏的URL来显示我调试控制台),现在我试图把它放到web上,我遇到了麻烦

我该如何将其写入文档?我现在有点不知所措

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);
}
}
}
public部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共bool url是有效的(字符串url)
{
尝试
{
HttpWebRequest request=HttpWebRequest.Create(url)为HttpWebRequest;
request.Timeout=5000;//将超时设置为5秒,以避免用户等待页面加载的时间过长
request.Method=“HEAD”//只获取标题信息——无需下载任何内容
HttpWebResponse=request.GetResponse()作为HttpWebResponse;
int statusCode=(int)response.statusCode;
if(statusCode>=100&&statusCode<400)//良好的请求
{
返回true;
}

否则,如果(statusCode>=500&&statusCode我认为您需要
Response.Write()
代替
Debug.WriteLine()
方法。或者,您可以在标记中创建一个TextArea对象,并使用
myTextArea.Text+=“一些文本”

您如何调用
UrlIsValid
?我没有注意到我没有在帖子中包含它,下面是它的调用方式(它位于“按钮”的底部:if(UrlIsValid(validator)==false){Debug.WriteLine(validator);}checker=checker.子字符串(checkstop+1);