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是否显示无法显示网页表示服务器已关闭