【问题标题】:Variable not within scope using HttpWebRequest and WebResponse变量不在使用 HttpWebRequest 和 WebResponse 的范围内
【发布时间】:2015-10-06 18:47:39
【问题描述】:

我的变量 httpRes 有问题。基本上这是一个命令行应用程序的基础知识,它将检查一组给定的 URL 并返回它们的状态代码,即未经授权、重定向、正常等。问题是我列表中的一个应用程序不断抛出错误。所以我使用了 try catch 子句来捕获错误并告诉我是什么原因造成的。

不幸的是,变量 httpRes 在 try 子句中有效,但在 catch 中无效。它一直被返回为空。我在 try/catch 语句之外调用了 httpRes,所以我希望我的范围是正确的,但无论出于何种原因,catch 语句的值永远不会从 null 更改,只有 try 语句。

这里是引用的代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace URLMonitor
{
    class Program
    {
        static void Main(string[] args)
       {
            string url1 = "https://google.com"; //removed internal URL for security reasons.
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url1);
            httpReq.AllowAutoRedirect = false;
            HttpWebResponse httpRes = null;

            try
            {
                httpRes = (HttpWebResponse)httpReq.GetResponse();
                if (httpRes.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Website is OK");
                    // Close the response.
                    //httpRes.Close();
                }
            }
            catch
            {
                if (httpRes != null)
                {
                    if (httpRes.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        Console.WriteLine("Things are not OK");
                        //httpRes.Close();
                    }
                }
                else
                {
                    Console.WriteLine("Please check the URL and try again..");
                    //httpRes.Close();
                }
            }
            Console.ReadLine();

        }
    }
}

【问题讨论】:

    标签: c# .net httpwebrequest httpwebresponse http-error


    【解决方案1】:

    如果你捕捉到一个异常,那可能是因为 GetResponse 失败了,对吧?所以你还没有为httpRes 分配任何东西......

    在我看来,您应该正在赶上WebException,此时您可以查看响应 - 如果有的话:

    catch (WebException e)
    {
        // We're assuming that if there *is* a response, it's an HttpWebResponse
        httpRes = (HttpWebResponse) e.Response;
        if (httpRes != null)
        {
            ...
        }
    }
    

    几乎从不值得写一个裸 catch 块,顺便说一句 - 总是至少捕获 Exception,但无论如何最好捕获更具体的异常类型,除非你在顶部您的应用程序堆栈级别。

    就我个人而言,我不会为两段代码使用相同的变量——我会在 try 块中声明成功案例的响应,并在 catch 块中声明失败案例的响应。另请注意,您通常应该处理您的WebResponse,例如

    using (var response = request.GetResponse())
    {
        // Use the response
    }
    

    如果GetResponse 抛出异常并且您从异常中获得响应,我认为您不需要这样做。

    【讨论】:

    • 这就是修复。所以显然我没有发现那个特定的错误。我假设一个通用的 catch all 会捕获任何异常,而不是我必须指定我试图捕获的异常。
    • @stack-flo:是的,它捕获任何异常(它捕获了抛出的异常)-但是您在没有指定变量的情况下捕获了它,这意味着您无法使用异常中的信息...并且如果不指定要使用的异常类型,则无法使用特定于异常类型的属性。
    • 知道了。感谢您的详尽解释!
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多