MFC HTTP GET 请求
//HTTP GET 下载文件 CInternetSession *pInetSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); CHttpConnection *pHttpConn = NULL; CHttpFile *pHttpFile = NULL; CString strUrl; DWORD dwServiceType; CString strServer, strObj, strUser, strPwd; INTERNET_PORT nPort; strUrl = "http://www.tdx.com.cn/products/data/data/dbf/base.zip"; if (!AfxParseURLEx(strUrl, dwServiceType, strServer, strObj, nPort, strUser, strPwd, ICU_NO_ENCODE)) { MessageBox("下载地址有误!..."); return; } if (!pHttpConn) { pHttpConn = pInetSession->GetHttpConnection(strServer, INTERNET_INVALID_PORT_NUMBER, "ELIMINATOR77","PANZER76"); } int nVal = 6000; pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, &nVal, sizeof(nVal)); pHttpFile = pHttpConn->OpenRequest("GET",strObj); try { MessageBox(_T("正在连接到网站...")); pHttpFile->SendRequest(); } catch (CInternetException *pEx) { MessageBox("下载出错..."); pEx->Delete(); delete pHttpFile; return; } DWORD dwRet; pHttpFile->QueryInfoStatusCode(dwRet); if (dwRet != HTTP_STATUS_OK) { CString error; error.Format("出错了,错误码:%d",dwRet); AfxMessageBox(error); return; } char szPath[100]={0}; GetCurrentDirectory(sizeof(szPath),szPath); CString HomePath; HomePath = szPath; int p1 = HomePath.ReverseFind('/'); int p2 = HomePath.ReverseFind('\\'); int pos = max(p1,p2); if (pos > 0) { HomePath = HomePath.Left(pos+1); } CString filePath; filePath.Format("%s%s",HomePath,"base.zip"); //TCHAR* pszFileName = _T("Open_File.dat"); CFile tempFile; CFileException fe; //CFile tempFile(filePath, CFile::modeCreate|CFile::modeWrite); if (!tempFile.Open(filePath, CFile::modeCreate|CFile::modeWrite, &fe)) { DWORD erno = GetLastError(); CString error; TCHAR szErr[512]={0}; fe.GetErrorMessage(szErr,sizeof(szErr)); error.Format("不能将下载文件存盘:%s",szErr); AfxMessageBox(error); //return; } int numread = 0; char buf[512]={0}; while ((numread=pHttpFile->Read(buf,sizeof(buf)-1)) > 0) { tempFile.Write(buf,numread); } tempFile.Close(); pInetSession->Close(); delete pInetSession; pHttpFile->Close(); delete pHttpFile;