Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 如何忽略webrequest中的401未授权错误以获取网站状态_C#_.net_Httpwebrequest - Fatal编程技术网

C# 如何忽略webrequest中的401未授权错误以获取网站状态

C# 如何忽略webrequest中的401未授权错误以获取网站状态,c#,.net,httpwebrequest,C#,.net,Httpwebrequest,我正在编写一个应用程序来检查一些内部web应用程序的状态。其中一些应用程序使用Windows身份验证。当我使用此代码检查状态时,它抛出远程服务器返回错误:(401)未经授权。。这是可以理解的,因为我没有向webiste提供任何凭据,所以我没有获得授权。 有没有办法不做类似的事情就忽略401错误? 当服务器关闭或无法访问时,将出现超时异常。我知道解决这个问题的唯一方法是尝试/抓住 我很确定大多数错误(401/404/501)都是这样,所以:不,你不能忽略(防止)异常,但你必须处理它们。它们是

我正在编写一个应用程序来检查一些内部web应用程序的状态。其中一些应用程序使用Windows身份验证。当我使用此代码检查状态时,它抛出
远程服务器返回错误:(401)未经授权。
。这是可以理解的,因为我没有向webiste提供任何凭据,所以我没有获得授权。


有没有办法不做类似的事情就忽略401错误?


当服务器关闭或无法访问时,将出现超时异常。我知道解决这个问题的唯一方法是尝试/抓住


我很确定大多数错误(401/404/501)都是这样,所以:不,你不能忽略(防止)异常,但你必须处理它们。它们是获取应用程序正在查找的大多数状态代码的唯一方法。

不足之处在于,您需要检查myHttpWebResponse.StatusCode中的状态代码并相应地采取行动

示例代码来自:


我建议你试试这个:

        try
        {
            objResponse = objRequest.GetResponse() as HttpWebResponse;
        }
        catch (WebException ex)
        {
            objResponse = ex.Response as HttpWebResponse;
        }
        finally

WebException会响应您想要的所有信息。

此处的“忽略”是什么意思,请转到下一页?它可能对异常毫无帮助,但是你应该做一个HEAD请求,因为它比较轻,并且处理得很好。@HenkHolterman要么是一个使webrequest不会抛出异常的属性,要么就是像我上面所说的那样吞下异常。@M.Babcock你有一个链接到一个示例或者如何使用它吗?@guanome-,尽管唯一重要的一行是
request.Method=“HEAD”
异常在
objResponse=objRequest.GetResponse()处抛出,因此无法检查响应,因为从未设置响应。如果您的意思是在异常中,它将给我一个
协议错误
,因此我仍然需要筛选异常消息,以查看状态代码实际上是什么。
WebRequest objRequest = HttpWebRequest.Create(website);

try
{
    objResponse = objRequest.GetResponse();
}
catch (WebException ex)
{
    //Catch and ignore 401 Unauthorized errors because this means the site is up, the app just doesn't have authorization to use it.
    if (!ex.Message.Contains("The remote server returned an error: (401) Unauthorized."))
    {
        throw;
    }                    
}
public static void GetPage(String url) 
    {
        try 
          { 
                // Creates an HttpWebRequest for the specified URL. 
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
                // Sends the HttpWebRequest and waits for a response.
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                   Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
                                        myHttpWebResponse.StatusDescription);
                // Releases the resources of the response.
                myHttpWebResponse.Close(); 

            } 
        catch(WebException e) 
           {
                Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); 
           }
        catch(Exception e)
        {
            Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
        }
    }
        try
        {
            objResponse = objRequest.GetResponse() as HttpWebResponse;
        }
        catch (WebException ex)
        {
            objResponse = ex.Response as HttpWebResponse;
        }
        finally