【问题标题】:Handle HttpStatusCode inside if statement, not try/catch在 if 语句中处理 HttpStatusCode,而不是 try/catch
【发布时间】:2014-12-11 04:19:59
【问题描述】:

此代码必须根据页码找到有效页面。这应该实现逻辑 - 如果地址为http://hostname/page/x 的页面返回 OK 状态,则 url 有效,如果响应状态 NotFound,则 urls 无效。我想在 if 语句中处理 HttpStatusCode,但它总是被 try/catch 块捕获。

    public void GetAllLinks()
            {
                const string baselink = "http://buzzon.khaleejtimes.com/ad-category/real-estate/page/";
                //Check if OK status answer from server, page link is valid
                for (int i = 1; i < 10000; i++)
                {
                    var url = baselink + i;
                    if (LinkExist(url) != true)
                    {
                      try {
                        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                        // Sends the HttpWebRequest and waits for a response.
                        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                        if (myHttpWebResponse.StatusCode == HttpStatusCode.NotFound)
                        {  
                            //Exit loop if status 404.  
                            break;
                        }
                        WriteUrl(url);
                        myHttpWebResponse.Close();
                  }
                  catch (Exception ex) {
                     throw new Exception(ex.Message);
                   } 
                }
                ParsePages();
        } 

【问题讨论】:

    标签: c# try-catch http-status-codes


    【解决方案1】:

    试试这个

     public string GetAllLinks()
                {
                    const string baselink = "http://buzzon.khaleejtimes.com/ad-category/real-estate/page/";
                    //Check if OK status answer from server, page link is valid
                    for (int i = 1; i < 10000; i++)
                    {
                        var url = baselink + i;
                        if (LinkExist(url) != true)
                        {
                          try 
                          {
                            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                            // Sends the HttpWebRequest and waits for a response.
                            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                          using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                          {
                            if (HttpStatusCode.OK == response.StatusCode)
                            {                           
                              WriteUrl(url);
                              myHttpWebResponse.Close();
                            }
                            else
                               break;
                         }
                      }
                      catch (Exception wex) 
                      {
                         string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
                         return pageContent;
                       } 
                    }
                    ParsePages();
                    return "Response Established";
            }
    

    【讨论】:

    • 不幸的是再次出现同样的问题 - 404 代码通过 try/catch 而不是 if 循环捕获。
    • Exception 中没有 Response 方法。你能再检查一下你的代码吗?
    • 现在检查代码,并确保调用函数必须调用此函数,例如 string GetResponse = GetAllLinks();Console.WriteLine();// 在此处显示字符串内容跨度>
    【解决方案2】:
    try adding a null check ..
    
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    var temp = myHttpWebRequest.GetResponse();
    
    if (temp == null)
    {
             //break code
    }
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) temp;
    

    【讨论】:

    • 不会有空答案。我想总会有一些 StatusCode。
    【解决方案3】:

    好吧,如果有人提供更好的解决方案,我会很高兴,但现在这个正在工作:

                        catch (WebException ex)
                        {
                            var resp = (HttpWebResponse) ex.Response;
                            if (resp.StatusCode == HttpStatusCode.NotFound)
                            break;
                        }
    

    我不喜欢这个解决方案,因为它不漂亮。因此,如果有人有更好的想法 - 欢迎您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-31
      • 2018-01-27
      • 2022-01-21
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多