unit InternetTime; interface uses System.Net.HttpClientComponent, System.SysUtilS, System.DateUtils, Winapi.Windows; function GetInternetTime(var _Time: TDateTime): Boolean; function InternetTimeToSystemTimeW(pSour: LPWSTR; var pst: SYSTEMTIME; dwReserved: DWORD): BOOL; stdcall; external 'wininet.dll'; implementation function GetInternetTime(var _Time: TDateTime): Boolean; var NetHTTPClient: TNetHTTPClient; _InternetTime: string; _SystemTime: SYSTEMTIME; begin Result := False; try NetHTTPClient := TNetHTTPClient.Create(nil); try NetHTTPClient.ConnectionTimeout := 3000; _InternetTime := NetHTTPClient.Head('https://www.baidu.com/').GetHeaderValue('Date'); finally NetHTTPClient.Free; end; if _InternetTime <> '' then begin _SystemTime := default(SYSTEMTIME); if InternetTimeToSystemTimeW(@_InternetTime[1], _SystemTime, 0) then begin _Time := IncHour(SystemTimeToDateTime(_SystemTime), 8); Result := True; end; end; except end; end; end.
调用百度接口基本不会出问题