//added by zhanganzhan on 20090317 to open a url on 20090317 BOOL UpLoad(LPCSTR URL)//, BOOL& quit, CSocketList& list, CMutex& mutex) { 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)) return FALSE; try{ pRequestHeader = HttpSocket.FormatRequestHeader((LPTSTR)(LPCTSTR)strServer,(LPTSTR)(LPCTSTR)strObject,nLength, nPort); if(!HttpSocket.Socket()) throw 0; tmpS = HttpSocket.GetSocket(); if(!HttpSocket.Connect((LPTSTR)(LPCTSTR)strServer,nPort)) throw 1; if(!HttpSocket.SendRequest()) throw 1; int nLineSize = 0; char szLine[256]; while(nLineSize != -1) nLineSize = HttpSocket.GetResponseLine(szLine,256); HttpSocket.CloseSocket(); } catch(int err){ // POSITION pos; // CSingleLock SingleLock(&mutex); switch(err){ // case 3: // DownloadFile.Close(); case 2: case 1: HttpSocket.CloseSocket(); break; } return FALSE; } return TRUE; } catch(...){ #ifdef _DEBUG AfxMessageBox("BOOL DownLoad(LPCSTR URL, LPCSTR Path, BOOL& quit, CSocketList& list)"); #endif } return FALSE; }
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; }