联网获取时间
1:服务器架构: 以PHP为例 <?php echo $showtime=date("Y-m-d H:i:s");?> 2:C++代码 #define MAXBLOCKSIZE 1024 #include <wininet.h> #pragma comment( lib, "wininet.lib" ) #include <afxinet.h> /*将Url指向的地址的文件下载到save_as指向的本地文件*/ BOOL download(const TCHAR *Url,const TCHAR *save_as) { BOOL bError=FALSE; byte Temp[MAXBLOCKSIZE]; ULONG Number = 1; ULONG Total=0; HINTERNET hSession = InternetOpen((LPCWSTR)_T("RookIE/1.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hSession != NULL) { HINTERNET handle2 = InternetOpenUrl(hSession, (LPCWSTR)Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); if (handle2 != NULL) { CFile file; if(file.Ope(save_as,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) { while (Number > 0) { InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number); if(Number>0) { Total+=Number; file.Write(Temp, Number); // 将实际数据写入文件 } } file.Close(); } else { bError=TRUE; } InternetCloseHandle(handle2); handle2 = NULL; } else { bError=TRUE; } InternetCloseHandle(hSession); hSession = NULL; return !bError; } else { return FALSE; } } void CGetCurTimeDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 TCHAR lpFile[MAX_PATH+1]={0}; GetTempPath(MAX_PATH,lpFile); PathAppend(lpFile,_T("CurTime.txt")); if(!download(_T("http://www.cadgj.com/curtime.php"),lpFile)) { AfxMessageBox(_T("联网下载时间文件失败")); return; } try { CFile file; if(!file.Open(lpFile,CFile::modeRead|CFile::typeBinary)) { AfxMessageBox(_T("读取时间文件失败"),MB_OK); return; } byte* bytes=new byte[file.GetLength()+1]; memset(bytes,0,file.GetLength()+1); file.Read(bytes,file.GetLength()); CString strText=CString((const char*)bytes); delete[] bytes; bytes=NULL; file.Close(); COleDateTime dt1; if(!dt1.ParseDateTime(strText)) { AfxMessageBox(_T("解析时间文件失败"),MB_OK); return; } CString strDateTime; strDateTime.Format(_T("%.4d年%.2d月%.2d日%.2d时%.2d分%.2d秒"),dt1.GetYear(),dt1.GetMonth(),dt1.GetDay(),dt1.GetHour(),dt1.GetMinute(),dt1.GetSecond()); AfxMessageBox(strDateTime); } catch(...) { AfxMessageBox(_T("读取文件发生异常"),MB_OK); } //OnOK(); }查看完整版本: 联网获取时间
Tags: