WebRequest检查服务器

时间:2012-10-19 17:05:24

标签: javascript asp.net html vb.net httpwebrequest

我正在尝试检查我的服务器(本地服务器)是启动还是关闭。 我想传递我的服务器名称:myDEVServer12(http://www.example.com/ss.asp)

Public Function CheckServer(ByVal ServerName As String) As Boolean
    Try
        Dim request As WebRequest = WebRequest.Create(ServerName)
        Dim response As WebResponse = request.GetResponse()
        imgServer1.ImageUrl = ("~/Images/green_light.png")
    Catch ex As Exception
        imgServer1.ImageUrl = ("~/Images/red_light.png")
        Return False
    End Try
    Return True
End Function

我也试过用ping。它可以工作,但页面需要很长时间才能加载(4台服务器)

Dim ping As New Ping
Try
    Dim pingreply = ping.Send(lblServer1.Text, 2000)
    If pingreply.Status = IPStatus.Success Then
        imgServer1.ImageUrl = ("~/Images/green_light.png")
    Else
        imgServer1.ImageUrl = ("~/Images/red_light.png")
    End If
Catch ex As Exception
    imgServer1.ImageUrl = ("~/Images/red_light.png")
End Try

1 个答案:

答案 0 :(得分:2)

Dim myRequest As WebRequest = WebRequest.Create("http://www.google.com")
' Return the response. 
Dim myResponse As String = myRequest.GetResponse().ContentType().ToString()
MsgBox(myResponse)