//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; }
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; }
int CTestHttpDlg::ThreadFunc() { //CFileDialog FileDlg(FALSE); CHttpSocket sock; BOOL IFOK = FALSE; IFOK = sock.Socket(); if(!IFOK) { ASSERT(FALSE); return 0; } sock.SetTimeout(10,0); IFOK = sock.Connect((LPTSTR)(LPCTSTR)m_strServer, m_port); if(!IFOK) { ASSERT(FALSE); return 0; } //set reqestheader string strsend = m_strRequest; sock.SetRequest(strsend); //sock.SendRequest(strsend.c_str(), strsend.length() ); sock.SendRequest( ); int nLineSize = 0; char szLine[256+1]; memset(szLine, 0, 256+1); m_ctrlList.ResetContent(); while(nLineSize != -1) { nLineSize = sock.GetResponseLine(szLine,256); if(nLineSize > -1) { szLine[nLineSize] = '\0'; m_ctrlList.AddString(szLine); } } //char szValue[30]; //HttpSocket.GetField("Content-Length",szValue,30); string strContent = sock.GetContent(); int pos = strContent.find("["); string strJson = strContent.substr(pos + 1, strContent.length()-2 ); int nFileSize = strContent.length(); if(strJson.length() ){ //JSONNode* node = (JSONNode*)json_parse(strJson.c_str() ); //if(!node) // return 0; //char jsontype = json_type(node); //if(JSON_NODE == jsontype) //{} //int nodesize = json_size(node); //json_char * res = json_as_string(json_at(node, 0)); } //bool ifok = HttpSocket.GetContentLength(nFileSize); int nSvrState = sock.GetServerState(); //int nFileSize = atoi(szValue); m_ctrlProgress.ShowWindow(SW_SHOW); m_ctrlProgress.SetRange(0,nFileSize / 1024); //bool ifwriteok = this->WriteFile(sock, nFileSize); m_ctrlProgress.ShowWindow(SW_HIDE); m_ctrlProgress.SetPos(0); string sdf; sdf.push_back('d'); sock.CloseSocket(); return 0; }
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; }