如何使用 .NET、C# 和 WPF 检查 Internet 连接

How to check the Internet connection with .NET, C#, and WPF(如何使用 .NET、C# 和 WPF 检查 Internet 连接)
本文介绍了如何使用 .NET、C# 和 WPF 检查 Internet 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET、C# 和 WPF,我需要检查是否打开了到某个 URL 的连接,但我在 Internet 上找到的任何代码都无法运行.

I am using .NET, C# and WPF, and I need to check whether the connection is opened to a certain URL, and I can't get any code to work that I have found on the Internet.

我试过了:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
    IAsyncResult result = socket.BeginConnect("localhost/myfolder/", 80, null, null);
    bool success = result.AsyncWaitHandle.WaitOne(3000, true);
    if (!success)
    {
        MessageBox.Show("Web Service is down!");
    }
    else
        MessageBox.Show("Everything seems ok");
}
finally
{
    socket.Close();
}

但即使我关闭了本地 Apache 服务器,我总是会收到一切正常的消息.

But I always get the message that everything is OK even if I shut down my local Apache server.

我也试过了:

ing ping = new Ping();
PingReply reply;
try
{
    reply = ping.Send("localhost/myfolder/");
    if (reply.Status != IPStatus.Success)
        MessageBox.Show("The Internet connection is down!");
    else
        MessageBox.Show("Seems OK");
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message);
}

但这总是会出现异常(ping 似乎只能 ping 服务器,所以 localhost 有效,但 localhost/myfolder/没有)

But this always gives an exception (ping seems to work only pinging the server, so localhost works but localhost/myfolder/ doesnt)

请问如何检查连接以便它对我有用?

Please how to check the connection so it would work for me?

推荐答案

最后我用了自己的代码:

In the end I used my own code:

private bool CheckConnection(String URL)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Timeout = 5000;
        request.Credentials = CredentialCache.DefaultNetworkCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
            return true;
        else
            return false;
    }
    catch
    {
        return false;
    }
}

有趣的是,当服务器关闭时(我关闭了我的 Apache),我没有收到任何 HTTP 状态,但会引发异常.但这已经足够好了:)

An interesting thing is that when the server is down (I turn off my Apache) I'm not getting any HTTP status, but an exception is thrown. But this works good enough :)

这篇关于如何使用 .NET、C# 和 WPF 检查 Internet 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)