vb.net取网络时间的简单介绍

Vb.net 2008 如何获取网络时间

你是指Internet上的国际标准时间吗。

成都创新互联公司是一家集网站建设,洞口企业网站建设,洞口品牌网站建设,网站定制,洞口网站建设报价,网络营销,网络优化,洞口网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

先加入控件 AxWinsock1 在.Com中Microsoft.Winsock

Public Class Form1

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)

Dim NoSrv As Boolean

Dim TimeFromNet As String

Dim OldTime As Date

Dim NewTime As Date

Dim MyDate As Date

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()

AxWinsock1.Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol

NetTime("") '首先取中科院国家授时中心时间

If NoSrv Or TimeFromNet = "" Then

NetTime("time.nist.gov") '取美国标准技时院时间

If NoSrv Or TimeFromNet = "" Then

MsgBox("检测不到网络标准时间服务器time.nist.gov!")

Else

NetTime("time.nist.gov")

If TimeFromNet = "" Then

MsgBox("网络标准时间服务器time.nist.gov超时!")

Else

MyDate = Mid(TimeFromNet, 8, 8)

OldTime = Mid(TimeFromNet, 17, 8)

NewTime = TimeSerial((Hour(OldTime) + 8) Mod 24, Minute(OldTime), Second(OldTime))

Dim MyTime As DateTime = MyDate Space(1) NewTime

MsgBox(MyTime)

'SetWindowsClock(MyTime)

End If

End If

Else

'使网络误差时间小,第2次再中科院国家授时中心时间

NetTime("")

If TimeFromNet = "" Then

MsgBox("网络标准时间服务器超时!")

Else

MyDate = Mid(TimeFromNet, 8, 8)

NewTime = Mid(TimeFromNet, 17, 8)

Dim MyTime As DateTime = MyDate Space(1) NewTime

MsgBox(MyTime)

'SetWindowsClock(MyTime)

End If

End If

End Sub

Private Sub NetTime(ByVal TimeSrv As String)

NoSrv = False

TimeFromNet = ""

If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()

AxWinsock1.RemoteHost = TimeSrv ' "" 或 "time.nist.gov"

AxWinsock1.RemotePort = 13

AxWinsock1.LocalPort = 0

AxWinsock1.Connect()

Do While TimeFromNet = ""

If NoSrv Then Exit Do

Sleep(55)

System.Windows.Forms.Application.DoEvents()

Loop

If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()

End Sub

Private Sub AxAxWinsock1_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxWinsock1.CloseEvent

AxWinsock1.Close()

End Sub

Private Sub AxAxWinsock1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles AxWinsock1.DataArrival

AxWinsock1.GetData(TimeFromNet)

End Sub

Private Sub AxAxWinsock1_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles AxWinsock1.Error

NoSrv = True

End Sub

End Class

vb。net怎么获取网络上的时间 坐等

Dim obj,OBJStatus As Object,url As String,GetText As String,i As Integer

Dim Retrieval

url=""

'判断网络是否连接

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.StatusCode0 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.Readystate4 Then Exit Sub

GetText=.getAllResponseHeaders()

i=InStr(1,GetText,"date:",vbTextCompare)

If i0 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

vb.net获取网络时间失败

是不是另一台电脑是vista 及以上版本的系统 ?

用管理员模式。试试


标题名称:vb.net取网络时间的简单介绍
文章链接:http://ybzwz.com/article/ddoseed.html