int DownLoadFile(std::string serverName, std::string URL, std::string localDirectory) { FILE *fp; if (!(fp = fopen(localDirectory.c_str(), "wb+"))) { printf("can't open local file\n"); return -1; } CHttpSocket *cs = new CHttpSocket(); cs->Socket(); cs->Connect(serverName.c_str(), 80); long len = 0; std::string req = cs->FormatRequestHeader(serverName.c_str(), URL.c_str(), len, NULL, NULL, 0, 0, 0); cs->SendRequest(req.c_str(), len); int lens; std::string head = cs->GetResponseHeader(lens); printf("%s\n", head.c_str()); int cnt = 0; int flag = head.find("Content-Length:", 0); int endFlag = head.find("\r\n", flag); std::string subStr = head.substr(flag, endFlag - flag); sscanf(subStr.c_str(), "Content-Length: %d", &lens); fseek(fp, 0, 0); while (cnt < lens) { char buff[1025]; int tmplen = cs->Receive(buff, 1024); cnt += tmplen; fwrite(buff, 1, tmplen, fp); } fclose(fp); return 0; }
bool CTestHttpDlg::WriteFile(CHttpSocket& rHttpSocket, int fileSize) { int nCompletedSize = 0; //get log file name CString strFileName; strFileName = c_strLog; CFile DownloadFile; DownloadFile.Open(strFileName,CFile::modeCreate | CFile::modeWrite); char pData[1024]; int nReceSize = 0; DWORD dwStartTime,dwEndTime; while(nCompletedSize < fileSize) { dwStartTime = GetTickCount(); nReceSize = rHttpSocket.Receive(pData,1024); if(nReceSize == 0) { AfxMessageBox("Server has been shut down.."); break; } if(nReceSize == -1) { AfxMessageBox("receive timeout."); break; } dwEndTime = GetTickCount(); DownloadFile.Write(pData,nReceSize); nCompletedSize += nReceSize; m_ctrlProgress.SetPos(nCompletedSize / 1024); //Speed CString strSpeed; //strSpeed.Format("%d",dwStartTime -dwEndTime); strSpeed.Format("%d",nReceSize); m_stcSpeed.SetWindowText(strSpeed); } DownloadFile.Close(); return true; }
BOOL DownLoad(LPCSTR URL, LPCSTR Path, BOOL& quit, CSocketList& list) { try{ CHttpSocket HttpSocket; CFile DownloadFile; CString strServer,strObject; unsigned short nPort; DWORD dwServiceType; long nLength; const char *pRequestHeader = NULL; SOCKET tmpS = 0; if(!AfxParseURL(URL,dwServiceType,strServer,strObject,nPort)){ //AfxMessageBox("AfxParseURL"); return FALSE; } try{ pRequestHeader = HttpSocket.FormatRequestHeader((LPTSTR)(LPCTSTR)strServer,(LPTSTR)(LPCTSTR)strObject,nLength, nPort); if(!HttpSocket.Socket()) throw 0; tmpS = HttpSocket.GetSocket(); list.AddTail(tmpS); if(quit){ //AfxMessageBox("quit"); return FALSE; } // HttpSocket.SetTimeout(10000,0); if(!HttpSocket.Connect((LPTSTR)(LPCTSTR)strServer,nPort)) throw 1; if(quit) return FALSE; if(!HttpSocket.SendRequest()) throw 1; if(quit) return FALSE; int nLineSize = 0; char szLine[256]; while(nLineSize != -1){ nLineSize = HttpSocket.GetResponseLine(szLine,256); if(quit) return FALSE; } char szValue[30]; BOOL bContLen = TRUE; int nFileSize = -1; if(HttpSocket.GetField("Content-Length",szValue,30)==-1) bContLen = FALSE; else nFileSize = atoi(szValue); if(quit) return FALSE; // int nSvrState = HttpSocket.GetServerState(); int nCompletedSize = 0; if(!DownloadFile.Open(Path, CFile::modeCreate|CFile::modeWrite)) throw 2; if(quit) return FALSE; char pData[1024]; int nReceSize = 0; BOOL first = TRUE; while(!quit){ nReceSize = HttpSocket.Receive(pData,1024); if(quit) return FALSE; if(nReceSize == 0) break; if(nReceSize == -1) throw 3; if(first&&!bContLen){ char* temp = strstr(pData,"\n"); if(!temp) throw 3; DownloadFile.Write(temp+2,nReceSize-(temp+2-pData)); } else DownloadFile.Write(pData,nReceSize); nCompletedSize += nReceSize; first = FALSE; if(bContLen && nCompletedSize>=nFileSize) break; } if(!bContLen && !quit){ long len = (long)DownloadFile.GetLength(); DownloadFile.SetLength(len-7); } DownloadFile.Close(); POSITION pos = list.Find(tmpS); if(pos) list.RemoveAt(pos); HttpSocket.CloseSocket(); } catch(int err){ POSITION pos; switch(err){ case 3: DownloadFile.Close(); case 2: case 1: pos = list.Find(tmpS); if(pos) list.RemoveAt(pos); HttpSocket.CloseSocket(); break; } return FALSE; } return !quit; } catch(...){ #ifdef _DEBUG // AfxMessageBox("BOOL DownLoad(LPCSTR URL, LPCSTR Path, BOOL& quit, CSocketList& list)"); #endif } return FALSE; }