Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 用c语言捕获web浏览器输出#_C# - Fatal编程技术网

C# 用c语言捕获web浏览器输出#

C# 用c语言捕获web浏览器输出#,c#,C#,在控制台应用程序中,我需要捕获输出。有两种情况: Internet无法显示网页 互联网正在运行 我正在使用下面的代码 using(WebClient client = new WebClient()) { string pageData; try { pageData = client.DownloadString("https://google.com"); } catch (HttpListenerException e)

在控制台应用程序中,我需要捕获输出。有两种情况:

  • Internet无法显示网页
  • 互联网正在运行
我正在使用下面的代码

using(WebClient client = new WebClient())
{
    string pageData;
    try
    {
        pageData = client.DownloadString("https://google.com");
    }
    catch (HttpListenerException e)
    {
        Console.WriteLine("Exception is" + e);
    }

这里我需要应用一个条件,如果Internet Explorer显示“Internet Explorer无法显示网页”,那么它应该不显示连接。我需要捕获输出。

您需要捕获web客户端由于任何原因无法加载页面时引发的WebException。试试这个:

public static bool IsAlive(string url)
{
    bool isAlive = false;
    using (WebClient client = new WebClient())
    {
        try
        {
            var content = client.DownloadString(url);
            // if we got this far there was no error fetching the content
            isAlive = true;
        }
        catch (WebException ex)
        {
            // could not fetch page - can output reason here if required
            Console.WriteLine("Error when fetching {0}: {1}", url, ex);
        }

    }

    return isAlive;
}

有关详细信息,请参阅MSDN上的文档。

您需要捕获web客户端因任何原因无法加载页面时引发的WebException。试试这个:

public static bool IsAlive(string url)
{
    bool isAlive = false;
    using (WebClient client = new WebClient())
    {
        try
        {
            var content = client.DownloadString(url);
            // if we got this far there was no error fetching the content
            isAlive = true;
        }
        catch (WebException ex)
        {
            // could not fetch page - can output reason here if required
            Console.WriteLine("Error when fetching {0}: {1}", url, ex);
        }

    }

    return isAlive;
}

有关详细信息,请参阅MSDN上的文档。

您可以分析字符串以查看所需文本是否存在。是否下载网页内容或检查internet连接?顺便说一句,DownloadString不会抛出
HttpListenerException
异常。我需要检查连接,它实际上需要检查服务器是否启动。我将输入负载均衡器URL,并检查IE是否显示无法显示网页表示服务器已关闭。您可以分析字符串以查看所需文本是否存在。是否下载网页内容或检查internet连接?顺便说一句,DownloadString不会抛出
HttpListenerException
异常。我需要检查连接,它实际上需要检查服务器是否启动。我将输入负载均衡器URL,并检查IE是否显示无法显示网页表示服务器已关闭