如何从我的代码ping网站 [英] How to ping website from my code

查看:122
本文介绍了如何从我的代码ping网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个测试网络连接的小程序

所有我的工作都没问题,但我面临的唯一问题是代码

 Res = My.Computer.Network.Ping(txtComputerName.Text).ToString 



它在我的本地网络上工作正常并给我很好的结果

但是当我尝试ping一个网站时它给了我错误



请注意我可以通过ISA服务器2006访问互联网而不能将我的电脑直接连接到路由器



我尝试过:



这是整个功能

< pre> 尝试 
Dim Res 作为 Boolean

lblError.Text = String .Empty
StopPing = False
Me .PictureBox1.Image = ImageList1.Images( 0
.lblElpased.ForeColor = Color.MidnightBlue

Dim i As = 0
执行
Application.DoEvents()
如果 StopPing 然后 退出 D o
Threading.Thread.Sleep( 300
.PictureBox1.Image = ImageList1.Images( 0
Me .Refresh()
Threading.Thread.Sleep( 200
Application.DoEvents()
Mtim = Stopwatch.StartNew
Res = My。 Computer.Network.Ping(txtComputerName.Text).ToString
Mtim。停止()
.lblElpased.Text = 时间:& Mtim.ElapsedMilliseconds& ms

Dim hostInfo As IPHostEntry = Dns.GetHostEntry(txtComputerName.Text)
Me 。 lblError.Text = hostInfo.HostName& vbCrLf

如果 Res 那么
.PictureBox1.Image = ImageList1.Images( 1
Me .lblElpased.ForeColor = Color.DarkGreen

Dim index As 整数
对于 index = 0 hostInfo.AddressList.Length - 1
Me .lblError.Text& = hostInfo.AddressList(index).ToString& vbCrLf

下一步
否则
.PictureBox1.Image = ImageList1.Images( 2
.lblElpased.ForeColor = Color.DarkRed

Dim index As 整数
对于 index = 0 hostInfo.AddressList.Length - 1
Me .lblError.Text& = hostInfo.AddressList(index).ToString& vbCrLf

下一步
结束 如果
i + = 1
循环直到( (i> = NuPings.Value)(chkKeepPing.Checked = False ))
Catch ex As 异常
lblError.Text = ex.Message
结束 尝试

解决方案

引用:

请注意我可以通过ISA服务器2006访问互联网,无法将我的电脑直接连接到路由器

在这种情况下,您只能使用服务器允许(转发)的协议。对于ping,这要求不过滤传出的ICMP数据包和相应的响应。你可能会问ISA服务器的管理员这个。



你不能ping一个网站。 Ping()函数的参数必须是IP地址, www.example.com 等URL或系统的计算机名称在您的本地网络中。



另请参阅Network.Ping Method(Microsoft.VisualBasic.Devices)| Microsoft Docs [ ^ ]:

Quote:

Ping方法不是用于确定远程计算机可用性的故障安全方法:目标计算机上的ping端口可能已关闭,或者ping请求可能被防火墙或路由器阻止。



传递给Ping方法的地址必须是DNS可解析的,并且不能以http://开头。

这意味着即使ICMP ping数据包不是,也可能没有响应由ISA服务器过滤。



引用:

我做的很小测试网络连接的程序

根据网络连接的含义,这可以通过其他方法完成:



检查系统网络接口的状态( NetworkInterface.OperationalStatus Property(System.Net.NetworkInformation)| Microsoft Docs [ ^ ])。



使用Dns.GetHostEntry Method(System.Net)| Microsoft Docs [ ^ ]。这将检查与为您的网络接口配置的其中一个DNS解析器的连接。这可能是ISA服务器,路由器或网络中的DNS服务器,并告诉您是否存在与该系统的连接。



用于互联网连接(这将是ISA服务器或路由器的一个问题,如果不可用)尝试连接到一个服务,如HTTP服务器没有被ISA服务器阻止的已知服务器。





还有一个Windows API函数返回连接状态: INetworkListManager :: GetConnectivity | Microsoft Docs [ ^ ]



不幸的是,似乎没有.Net等价物。但网络应提供如何从.Net调用它的示例,如如何使用Windows NLM API获得新网络连接的通知 [ ^ ]。

[/ EDIT]


I'm doing a small program for testing network connectivity
All My work is ok but the only problem I'm facing is the code

Res = My.Computer.Network.Ping(txtComputerName.Text).ToString


it's working ok on my local network and give me good results
but when I try to ping a web site it gives me error

note that I can access the internet through an ISA server 2006 and can't connect my computer directly to the router

What I have tried:

This is the entire function

<pre>     Try
            Dim Res As Boolean

            lblError.Text = String.Empty
            StopPing = False
            Me.PictureBox1.Image = ImageList1.Images(0)
            Me.lblElpased.ForeColor = Color.MidnightBlue

            Dim i As Short = 0
            Do
                Application.DoEvents()
                If StopPing Then Exit Do
                Threading.Thread.Sleep(300)
                Me.PictureBox1.Image = ImageList1.Images(0)
                Me.Refresh()
                Threading.Thread.Sleep(200)
                Application.DoEvents()
                Mtim = Stopwatch.StartNew
                Res = My.Computer.Network.Ping(txtComputerName.Text).ToString
                Mtim.Stop()
                Me.lblElpased.Text = "Time: " & Mtim.ElapsedMilliseconds & " ms"

                Dim hostInfo As IPHostEntry = Dns.GetHostEntry(txtComputerName.Text)
                Me.lblError.Text = hostInfo.HostName & vbCrLf

                If Res Then
                    Me.PictureBox1.Image = ImageList1.Images(1)
                    Me.lblElpased.ForeColor = Color.DarkGreen

                    Dim index As Integer
                    For index = 0 To hostInfo.AddressList.Length - 1
                        Me.lblError.Text &= hostInfo.AddressList(index).ToString & vbCrLf

                    Next
                Else
                    Me.PictureBox1.Image = ImageList1.Images(2)
                    Me.lblElpased.ForeColor = Color.DarkRed

                    Dim index As Integer
                    For index = 0 To hostInfo.AddressList.Length - 1
                        Me.lblError.Text &= hostInfo.AddressList(index).ToString & vbCrLf

                    Next
                End If
                i += 1
            Loop Until ((i >= NuPings.Value) And (chkKeepPing.Checked = False))
        Catch ex As Exception
            lblError.Text = ex.Message
        End Try

解决方案

Quote:

note that I can access the internet through an ISA server 2006 and can't connect my computer directly to the router

In such cases you can only use protocols that are allowed (forwarded) by the server. For ping this requires that outgoing ICMP packets and the corresponding responses are not filtered. You might ask the administrator of the ISA server about this.

You can't ping a website. The parameter for the Ping() function must be an IP address, an URL like www.example.com, or a computer name of a system in your local network.

See also Network.Ping Method (Microsoft.VisualBasic.Devices) | Microsoft Docs[^]:

Quote:

The Ping method is not a fail-safe method for determining the availability of a remote computer: the ping port on the target computer may be turned off or the ping request may be blocked by a firewall or router.

The address passed to the Ping method must be DNS resolvable and cannot be preceded by "http://".

That means that you might get no response even when ICMP ping packets are not filtered by the ISA server.

Quote:

I'm doing a small program for testing network connectivity

Depending on what you mean by "network connectivity" this can be done by other methods:

Check the status of your systems network interface (NetworkInterface.OperationalStatus Property (System.Net.NetworkInformation) | Microsoft Docs[^]).

Perform a name resolution using Dns.GetHostEntry Method (System.Net) | Microsoft Docs[^] with a valid name. This will check the connection to one of the DNS resolvers configured for your network interface. This might be either the ISA server, the router, or a DNS server in the net and tells you so if connectivity to that system is present.

For internet connectivity (which would be a problem of the ISA server or router if not available) try to connect to a service like HTTP of a known server that is not blocked by the ISA server.

[EDIT]
There is also a Windows API function returning the connection state: INetworkListManager::GetConnectivity | Microsoft Docs[^]

Unfortunately there seems to be no .Net equivalent. But the web should provides examples on how to call it from .Net like How to use the Windows NLM API to get notified of new network connectivity[^].
[/EDIT]


这篇关于如何从我的代码ping网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆