我在创建一个链接检查器时遇到了问题,我想把它放在网上主要是为了学习。
问题是我第一次使用它是作为一个控制台应用程序,它工作得很好(我得到了损坏的urls来表示我调试控制台),现在我试图把它放到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);
}
}
}希望你能理解我想要完成的事情,现在很难理解..
发布于 2012-03-16 20:53:07
我认为您希望用Response.Write()代替您的Debug.WriteLine()方法。或者,您可以在标记中创建TextArea对象并使用myTextArea.Text += "Some text";
https://stackoverflow.com/questions/9737512
复制相似问题