小编use*_*303的帖子

如何在c#中验证URL(错误404)

我需要编写一个工具,用C#报告损坏的URL.如果用户在浏览器中看到404错误,则URL应仅报告已损坏.我相信可能有一些技巧来处理进行URL重写的Web服务器.这就是我所拥有的.正如您所看到的,只有一些URL验证不正确.

    string url = "";

    // TEST CASES
    //url = "http://newsroom.lds.org/ldsnewsroom/eng/news-releases-stories/local-churches-teach-how-to-plan-for-disasters";   //Prints "BROKEN", although this is getting re-written to good url below.
    //url = "http://beta-newsroom.lds.org/article/local-churches-teach-how-to-plan-for-disasters";  // Prints "GOOD"
    //url = "http://";     //Prints "BROKEN"
    //url = "google.com";     //Prints "BROKEN" althought this should be good.
    //url = "www.google.com";     //Prints "BROKEN" althought this should be good.
    //url = "http://www.google.com";     //Prints "GOOD"

    try
    {

        if (url != "")
        {
            WebRequest Irequest = WebRequest.Create(url);
            WebResponse Iresponse = Irequest.GetResponse();
            if (Iresponse != null)
            {
                _txbl.Text = …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×1