我在我的代码中调用了一个web服务。我想添加验证以检查互联网连接是否可用。
如果互联网连接不可用,则应提示互联网连接不可用。
请提供检查此代码的代码。
答案 0 :(得分:0)
尝试ping目标服务器并查看结果,如果您收到回复,请继续请求 请参阅以下网址http://www.aspnettutorials.com/tutorials/network/net-ping-aspnet2-vb.aspx
答案 1 :(得分:0)
使用此功能可以抓取网页,调整超时以满足您的需求。这假设网站返回有效的200请求,如果网站已关闭或返回404或您可能需要调整的内容。
Function GetPage(inURL) ' scrapes the URL page, HTML, XML, JSON, whatever
Dim O(2), xmlServerHttp, error, ServerStatus, responseText
Error = False
Set xmlServerHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
lResolve = 1000
lConnect = 3500
lSend = 1000
lReceive = 2000
xmlServerHttp.setTimeouts lResolve, lConnect, lSend, lReceive
On Error Resume Next
xmlServerHttp.open "GET", inURL, false
xmlServerHttp.send()
If Err.Number <> 0 Then
ServerStatus="timeout"
error=True
Else
ServerStatus = xmlServerHttp.status
If ServerStatus = 200 Then responseText=xmlServerHttp.responseText Else error=true
End If
On Error GoTo 0
O(0) = responseText
O(1) = Error
O(2) = ServerStatus
Set xmlServerHttp = nothing
GetPage=O
End function
用法:
data = GetPage(url)
body = data(0)
error = data(1)
ServerStatus = data(2)
if error then
response.write "Sorry no internet"
else
response.write "Internet found!"
end if