vb。net怎么获取网络上的时间 坐等
发布网友
发布时间:2023-07-13 02:05
我来回答
共1个回答
热心网友
时间:2023-07-13 17:15
Dim obj,OBJStatus As Object,url As String,GetText As String,i As Integer
Dim Retrieval
url="http://www.163.com"
'判断网络是否连接
If url<>""Then
Retrieval=GetObject("winmgmts:\\.\root\cimv2")
obj=Retrieval.ExecQuery("Select*FromWin32_PingStatusWhereAddress='"&Mid(url,8)&"'")
For Each OBJStatus In obj
If IsNothing(OBJStatus.StatusCode) Or OBJStatus.StatusCode<>0 Then
Exit Sub
Else
Exit For'已连接则继续
End If
Next
End If
'通过下载网页头信息获取网络时间
Retrieval=CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open ( "Get",url,False,"","")
.setRequestHeader ("If-Modified-Since","0")
.setRequestHeader ("Cache-Control","no-cache")
.setRequestHeader ("Connection","close")
.Send()
If.Readystate<>4 Then Exit Sub
GetText=.getAllResponseHeaders()
i=InStr(1,GetText,"date:",vbTextCompare)
If i>0 Then'网页下载成功
i=InStr(i,GetText,",",vbTextCompare)
GetText= Trim(Mid(GetText,i+1))
i=InStr(1,GetText,"GMT",vbTextCompare)
GetText=GetText.Substring(0,i-1)' Left(GetText,i-1)
MsgBox ("网络时间:"&GetText)
End If
End With
Retrieval=Nothing
OBJStatus=Nothing
obj=Nothing