InternetReadFile 下载文件

  • 时间:
  • 来源:互联网
  • 文章标签:
internetreadfile 下载文件片段BOOL CEzxxxDlg::DownloadFile(CString &strIP,CString &strPort, CString &strUrl,CString &strFilename){HINTERNET hInternet = InternetOpen(_T("Microsoft Internet Explorer"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_INVALID_PORT_NUMBER );if(hInternet == NULL)return FALSE;HINTERNET hConnection = InternetConnect(hInternet,strIP, atoi(strPort.GetBuffer(10)),_T(""), _T(""), INTERNET_SERVICE_HTTP,0,0); if(hConnection==NULL){InternetCloseHandle(hInternet);return FALSE;}//请求urlCString strfiles = "\EzXXXXServer\\UpDateJCJ\\1.exe";HINTERNET hRequest = HttpOpenRequest(hConnection,_T("GET"), strfiles,//下载的urlHTTP_VERSION,NULL, 0,INTERNET_FLAG_NO_CACHE_WRITE,0);if(hRequest == NULL){MessageBox(_T("获取下载请求失败"),_T("警告"),MB_ICONWARNING);return FALSE;}// send requestif(!HttpSendRequest(hRequest, NULL, 0, 0, 0)){MessageBox(_T("发送下载请求失败"),_T("警告"),MB_ICONWARNING);InternetCloseHandle(hRequest);return FALSE;}CFile fNew;fNew.Open("JCJ2.exe",CFile::modeCreate|CFile::modeWrite);// allocate memoryDWORD initsize = 1024;DWORD bufsize = initsize;DWORD readsize = 0;BYTE *pBuf = (BYTE *)malloc(bufsize);ZeroMemory(pBuf, bufsize);DWORD nBytesRead;do{InternetReadFile(hRequest, pBuf + readsize, bufsize - readsize, &nBytesRead); // 缓冲器指针, 欲读数据的字节量。接收读取字节量的变量readsize += nBytesRead;if (readsize >= bufsize){// 重新分配内存bufsize += initsize;pBuf = (BYTE *)realloc(pBuf, bufsize);readsize = bufsize - initsize;}}while (nBytesRead > 0);pBuf[readsize] = NULL;fNew.Write(pBuf,readsize);fNew.Flush();InternetCloseHandle(hRequest);InternetCloseHandle(hConnection);InternetCloseHandle(hInternet);fNew.Flush();fNew.Close();MessageBox(_T("文件保存成功"),_T("提示"),MB_OK);}

本文链接http://www.taodudu.cc/news/show-647729.html