vb。net怎么获取网络上的时间 坐等 10
Dimobj,OBJStatus,url,GetText,iDimRetrievalurl="http://www.163.com"'判断网络是否连接Ifurl<>""T...
Dimobj,OBJStatus,url,GetText,i
DimRetrieval
url="http://www.163.com"
'判断网络是否连接
Ifurl<>""Then
SetRetrieval=GetObject("winmgmts:\\.\root\cimv2")
Setobj=Retrieval.ExecQuery("Select*FromWin32_PingStatusWhereAddress='"&Mid(url,8)&"'")
ForEachOBJStatusInobj
IfIsNull(OBJStatus.StatusCode)OrOBJStatus.StatusCode<>0Then
ExitSub
Else
ExitFor'已连接则继续
EndIf
Next
EndIf
'通过下载网页头信息获取网络时间
SetRetrieval=CreateObject("Microsoft.XMLHTTP")
WithRetrieval
.Open"Get",url,False,"",""
.setRequestHeader"If-Modified-Since","0"
.setRequestHeader"Cache-Control","no-cache"
.setRequestHeader"Connection","close"
.Send
If.Readystate<>4ThenExitSub
GetText=.getAllResponseHeaders()
i=InStr(1,GetText,"date:",vbTextCompare)
Ifi>0Then'网页下载成功
i=InStr(i,GetText,",",vbTextCompare)
GetText=Trim(Mid(GetText,i+1))
i=InStr(1,GetText,"GMT",vbTextCompare)
GetText=Left(GetText,i-1)
MsgBox"网络时间:"&GetText
EndIf
EndWith
SetRetrieval=Nothing
SetOBJStatus=Nothing
Setobj=Nothing
EndSub 展开
DimRetrieval
url="http://www.163.com"
'判断网络是否连接
Ifurl<>""Then
SetRetrieval=GetObject("winmgmts:\\.\root\cimv2")
Setobj=Retrieval.ExecQuery("Select*FromWin32_PingStatusWhereAddress='"&Mid(url,8)&"'")
ForEachOBJStatusInobj
IfIsNull(OBJStatus.StatusCode)OrOBJStatus.StatusCode<>0Then
ExitSub
Else
ExitFor'已连接则继续
EndIf
Next
EndIf
'通过下载网页头信息获取网络时间
SetRetrieval=CreateObject("Microsoft.XMLHTTP")
WithRetrieval
.Open"Get",url,False,"",""
.setRequestHeader"If-Modified-Since","0"
.setRequestHeader"Cache-Control","no-cache"
.setRequestHeader"Connection","close"
.Send
If.Readystate<>4ThenExitSub
GetText=.getAllResponseHeaders()
i=InStr(1,GetText,"date:",vbTextCompare)
Ifi>0Then'网页下载成功
i=InStr(i,GetText,",",vbTextCompare)
GetText=Trim(Mid(GetText,i+1))
i=InStr(1,GetText,"GMT",vbTextCompare)
GetText=Left(GetText,i-1)
MsgBox"网络时间:"&GetText
EndIf
EndWith
SetRetrieval=Nothing
SetOBJStatus=Nothing
Setobj=Nothing
EndSub 展开
1个回答
展开全部
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询