BOOL CFtpFileDlg::PutFile( CString strSourceName,CString strDesName ) { CInternetSession* pSession; CFtpConnection *pConnection; pConnection = NULL; pSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); try{ pConnection = pSession->GetFtpConnection(m_strFtpSite,m_strName,m_strPwd); } catch(CInternetException *e){ e->Delete(); pConnection = NULL; return FALSE; } if(pConnection != NULL){ if (!pConnection->PutFile(strSourceName,strDesName)){ pConnection->Close(); delete pConnection; delete pSession; return FALSE; } } if (pConnection != NULL){ pConnection->Close(); delete pConnection; } delete pSession; return TRUE; }
DWORD WINAPI sendFileThread(LPVOID lpParam) { char *filename = (char *)lpParam; try { char installPath[1024]; CModuleUtil::getInstallPath(installPath); char fname[1024]; sprintf(fname, "%s\\%s", installPath, filename); char fnameGz[1024]; sprintf(fnameGz, "%s\\%s.gz", installPath, filename); char remoteFileName[128]; sprintf(remoteFileName, "incoming/%s-%d-%d.gz", filename, time(NULL), rand()); file_compress(fname, "wb"); CInternetSession session; CFtpConnection *ftpConnection = session.GetFtpConnection("upload.tibiaauto.net", "anonymous", "*****@*****.**", 21, true); ftpConnection->PutFile(fnameGz, remoteFileName); ftpConnection->Close(); delete ftpConnection; _unlink(fname); _unlink(fnameGz); fileSendingProgress = 1; } catch (CInternetException*) { fileSendingProgress = -1; } return 0; }
bool CLoginDialog::pinReminder(){ CString username, email; int country=((CComboBox*) GetDlgItem(IDC_COUNTRY))->GetCurSel(); std::string userPhone=countries[country][1]; userPhone.erase(std::find(userPhone.begin(), userPhone.end(), '+')); userPhone.erase(std::find(userPhone.begin(), userPhone.end(), '-')); GetDlgItemText(IDC_PHONE,username); username=(CString)userPhone.c_str()+username; GetDlgItemText(IDC_EMAIL,email); std::string header = "/oneworld/forgotpin?number="; header+=(CT2CA)username; header+="&email="; header+=(CT2CA)email; CInternetSession session; CHttpConnection *pConnection = session.GetHttpConnection(_T("89.163.142.253")); char result[500]; CString request(header.c_str()); CHttpFile *pFile = pConnection->OpenRequest(1,request); if(!pFile->SendRequest()) return false; //pFile->QueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS,result,(LPDWORD)500); #ifdef _DEBUG pFile->Read((void*)result,500); _cprintf("%s",result); #endif return true; }
BOOL CMainFrame::DonwLoadFile(PSTR pURL, LPSTR SaveAsFilePath) { CInternetSession session; CHttpConnection* pServer = NULL; CHttpFile * pHttpFile = NULL; CString strServerName; //去掉http:// CString strObject; INTERNET_PORT nPort; DWORD dwServiceType; DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT; //请求标志 const TCHAR szHeaders[]=_T("Accept: text/*\r\nUser-Agent:HttpClient\r\n"); BOOL OK=AfxParseURL( pURL, dwServiceType, strServerName, strObject, nPort ); pServer = session.GetHttpConnection(strServerName, nPort); //获得服务器名 pHttpFile = pServer-> OpenRequest( CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags); pHttpFile->AddRequestHeaders(szHeaders); try { pHttpFile->SendRequest(); //发送请求 } catch (CInternetException* IE) { return false; } CStdioFile f; if( !f.Open( SaveAsFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary ) ) { return false; } TCHAR szBuf[1024]; int length=0; long a=pHttpFile->GetLength(); while (length=pHttpFile->Read(szBuf, 1023)) f.Write(szBuf,length); f.Close(); pHttpFile ->Close(); pServer ->Close(); if (pHttpFile != NULL) delete pHttpFile; if (pServer != NULL) delete pServer; session.Close(); return true; }
int CXCCRA2MapUpdaterApp::download_update(string link, string fname) { int error = 0; CInternetSession is; CHttpFile* f = reinterpret_cast<CHttpFile*>(is.OpenURL(link.c_str(), INTERNET_FLAG_TRANSFER_BINARY)); if (!f) error = 1; else { Cvirtual_file h; DWORD status; if (!f->QueryInfoStatusCode(status)) error = 2; else if (status != 200) error = 3; else { int total_size = f->Seek(0, CFile::end); f->Seek(0, CFile::begin); Cdownload_dlg dlg; dlg.set(link, fname, total_size); dlg.Create(Cdownload_dlg::IDD, NULL); dlg.EnableWindow(false); Cvirtual_binary t; while (!error) { int cb_p = min<int>(f->GetLength(), 1 << 10); if (!cb_p) break; f->Read(t.write_start(cb_p), cb_p); h.write(t); dlg.set_size(h.size()); dlg.UpdateWindow(); } h.compact(); Cxif_key k; if (k.load_key(h.data(), h.size())) error = 5; else { for (t_xif_key_map::const_iterator ki = k.m_keys.begin(); ki != k.m_keys.end(); ki++) { if (error) break; const Cxif_key& l = ki->second; string fext = boost::to_lower_copy(Cfname(l.get_value_string(vi_fname)).get_fext()); if (fext != ".mmx" && (fext != ".yro") || !Cfname(xcc_dirs::get_exe(game_ra2_yr)).exists()) continue; if (file32_write(Cfname(fname).get_path() + l.get_value_string(vi_fname), l.get_value(vi_fdata).get_data(), l.get_value(vi_fdata).get_size())) error = 6; } } dlg.DestroyWindow(); } f->Close(); } return error; }
void Conference::LoadList(){ CListCtrl *conf= (CListCtrl*)GetDlgItem(IDC_CONFLIST); conf->DeleteAllItems(); std::string header = "/oneworld/conf_list?api_token="; header+=((CmicrosipDlg*)GetParent())->getToken(); CInternetSession session; CHttpConnection *pConnection = session.GetHttpConnection(_T("89.163.142.253")); char result[500]; CString request(header.c_str()); CHttpFile *pFile = pConnection->OpenRequest(1,request); if(!pFile->SendRequest()) return; pFile->Read((void*)result,500); char* status = strchr(result,']'); //checking if data is receive and is parseable char* eom = strchr(result,'}'); #ifdef _DEBUG _cprintf("Size: %p, %p, %d\n",result, status, (status-result)); #endif if(status==NULL) result[eom-result+1]='\0'; else if(status - result < 4998) result[status - result +2]='\0'; #ifdef _DEBUG _cprintf("Result: %s\n",result); #endif cJSON *root = cJSON_Parse(result); cJSON *msg = cJSON_GetObjectItem(root,"msg"); if((status==NULL && msg == NULL) || status-result >= 4999 ) return; else if(status==NULL) return; cJSON *data = cJSON_GetObjectItem(root,"data"); int size=cJSON_GetArraySize(root); #ifdef _DEBUG _cprintf("Size: %d\n",size); #endif size=cJSON_GetArraySize(data); #ifdef _DEBUG _cprintf("Size: %d\n",size); #endif for(int i=0;i<size;i++){ cJSON* item= cJSON_GetArrayItem(data,i); CString confNum(cJSON_GetObjectItem(item,"confno")->valuestring); CString pin(cJSON_GetObjectItem(item,"pin")->valuestring); #ifdef _DEBUG _cprintf("Item: %s\n",(CT2CA)confNum); _cprintf("Pin: %s\n",(CT2CA)pin); #endif int index = conf->InsertItem(i, confNum); conf->SetItemText(index, 1, pin); //conf->SetItemData(i, &confNum); } }
void C51JobWebPost::TestProxy() { CInternetSession session; CHttpFile *file = NULL; INTERNET_PROXY_INFO proxyinfo; proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY; proxyinfo.lpszProxy ="122.205.95.14:80"; proxyinfo.lpszProxyBypass = NULL; session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO)); try{ file = (CHttpFile*)session.OpenURL("http://www.ip138.com/ip2city.asp",1, INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE); }catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); AfxMessageBox("CInternetException"); return; } CString strLine; CString strResult = ""; if(file != NULL){ while(file->ReadString(strLine) != NULL){ strResult += strLine; } }else{ AfxMessageBox("fail"); } file->Close(); session.Close(); }
DWORD CAsyncUrlReader::ThreadProc() { AfxSocketInit(nullptr); DWORD cmd = GetRequest(); if (cmd != CMD_INIT) { Reply((DWORD)E_FAIL); return (DWORD) - 1; } try { CInternetSession is; CAutoPtr<CStdioFile> fin(is.OpenURL(m_url, 1, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_CACHE_WRITE)); TCHAR path[MAX_PATH], fn[MAX_PATH]; CFile fout; if (GetTempPath(MAX_PATH, path) && GetTempFileName(path, _T("mpc_http"), 0, fn) && fout.Open(fn, modeCreate | modeWrite | shareDenyWrite | typeBinary)) { m_fn = fn; char buff[1024]; int len = fin->Read(buff, sizeof(buff)); if (len > 0) { fout.Write(buff, len); } Reply(S_OK); while (!CheckRequest(&cmd)) { int len2 = fin->Read(buff, sizeof(buff)); if (len2 > 0) { fout.Write(buff, len2); } } } else { Reply((DWORD)E_FAIL); } fin->Close(); // must close it because the destructor doesn't seem to do it and we will get an exception when "is" is destroying } catch (CInternetException* ie) { ie->Delete(); Reply((DWORD)E_FAIL); } // cmd = GetRequest(); ASSERT(cmd == CMD_EXIT); Reply(S_OK); // m_hThread = nullptr; return S_OK; }
bool CLoginDialog::registration(){ CString phone, name, email; int country=((CComboBox*) GetDlgItem(IDC_COUNTRY))->GetCurSel(); GetDlgItemText(IDC_PHONE,phone); GetDlgItemText(IDC_NAME,name); GetDlgItemText(IDC_EMAIL,email); std::string header = "/oneworld/webreg?country="; header+=countries[country][2]; header+="&number="; header+=(CT2CA)phone; header+="&name="; header+=(CT2CA)name; header+="&email="; header+=(CT2CA)email; header+="&countrycode="; header+=countries[country][2]; CInternetSession session; CHttpConnection *pConnection = session.GetHttpConnection(_T("89.163.142.253")); char result[500]; CString request(header.c_str()); CHttpFile *pFile = pConnection->OpenRequest(1,request); if(!pFile->SendRequest()) return false; //pFile->QueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS,result,(LPDWORD)500); #ifdef _DEBUG pFile->Read((void*)result,500); _cprintf("%s",result); #endif CString rest(result); int start=rest.Find(_T("success\":\"")); if(start<0) return false; start+=((CString)_T("success\":\"")).GetLength(); int end=rest.Find(_T("\""),start); if(end<0) return false; CString success=rest.Mid(start, end-start); #ifdef _DEBUG _cprintf("%s",(CT2CA)success); _cprintf("%s",result); #endif start=rest.Find(_T("msg\":\"")); start+=((CString)_T("msg\":\"")).GetLength(); end=rest.Find(_T("\""),start); CString msg=rest.Mid(start, end-start); SetDlgItemText(IDC_LOGTEXT,msg); Sleep(2000); return true; }
BOOL CAutoUpdateDlg::DownloadFile(LPCTSTR lpURL,LPCTSTR lpDestFile) { CFile cUdpFile; if(!cUdpFile.Open(lpDestFile,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary|CFile::shareDenyWrite)) return FALSE; BOOL bResult = FALSE; CInternetSession sessionDownload; try { CHttpFile* pFile = (CHttpFile*)sessionDownload.OpenURL(lpURL,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE); CString query = _T(""); pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,query); long file_len=_ttol(query); m_dwTotalSize = file_len; PostMessage(WM_POSMESSAGE,0,0); DWORD dwStatus; if (pFile->QueryInfoStatusCode(dwStatus)) { if (dwStatus == 200) { pFile->SetReadBufferSize(10240); if (TRUE) { DWORD dwLen = 0; char buf[BLOCKSIZE]; while (TRUE) { DWORD dwTemp = pFile->Read(buf,BLOCKSIZE); if (dwTemp) { cUdpFile.Write(buf,dwTemp); } m_dwDownloadedSize += dwTemp; PostMessage(WM_POSMESSAGE,0,0); if (dwTemp < BLOCKSIZE) { break; } } } } } pFile->Close(); bResult = TRUE; } catch(CInternetException* pException) { pException->Delete(); return bResult; } sessionDownload.Close(); cUdpFile.Close(); return bResult; }
int CXCCRA2MapUpdaterApp::update() { int error = 0; try { CWaitCursor wait; CInternetSession is; CHttpFile* f = reinterpret_cast<CHttpFile*>(is.OpenURL("http://xccu.sourceforge.net/ra2_maps/official.ucf")); if (!f) error = 1; else { string s; while (1) { int cb_p = f->GetLength(); if (!cb_p) break; char* p = new char[cb_p + 1]; f->Read(p, cb_p); p[cb_p] = 0; s += p; delete[] p; } f->Close(); Cvirtual_tfile f; f.load_data(Cvirtual_binary(s.c_str(), s.length())); while (!f.eof()) { Cmulti_line l = f.read_line(); Cfname fname = xcc_dirs::get_dir(game_ra2) + l.get_next_line('=') + ".mmx"; if (!fname.exists()) { string version = l.get_next_line(','); string link = l.get_next_line(','); error = download_update(link, fname); if (error) { delete_file(fname); MessageBox(NULL, "Error retrieving update.", NULL, MB_ICONERROR); error = 0; } } } } } catch (CInternetException*) { error = 1; } if (error) MessageBox(NULL, "Error querying for update.", NULL, MB_ICONERROR); return error; }
DWORD WINAPI LoadUpdate (LPVOID param) { CUpdateDlg* dlg = (CUpdateDlg*) param; if (dlg->autoCheck) UpdateVersion (NULL); char buf[2048]; sprintf (buf, "Current version: %s\r\n" "Last version: %s\r\n", formatVersion (curVersion), formatVersion (lastVersion)); dlg->SetDlgItemText (IDC_BUFFER, buf); if (curVersion < lastVersion) { CString log = buf; log += "\r\nLoading changelog..."; dlg->SetDlgItemText (IDC_BUFFER, log); try { CInternetSession inet; CInternetFile* file = dynamic_cast<CInternetFile*> (inet.OpenURL (logURL)); log = buf; log += "\r\nChangelog:\r\n"; if (file != NULL) { while (file->ReadString (buf, sizeof buf - 5)) { if (buf[0] == '*' && buf[1] == '*') { unsigned int ver = parseVersion (buf + 2); if (ver != 0 && ver <= curVersion) break; } log += buf; } log.Replace ("\n", "\r\n"); dlg->SetDlgItemText (IDC_BUFFER, log); delete file; } else lastVersion = 0; } catch (CInternetException*) { } } return 0; }
bool CUtil::IsReachableURL(CString sURL) { CInternetSession Session; CHttpFile * pFile; try { pFile = (CHttpFile *) Session.OpenURL(sURL); } catch (CException * e) { e->Delete(); pFile = NULL; } return (pFile != NULL); }
void CVersionChecker::VersionCheckerThread() { UINT totalBytesRead = 0; // The version tag usually shows up about 20,000 bytes in. char buffer[40000]; try { CInternetSession MySession; const wchar_t* const url = L"https://github.com/google/UIforETW/releases/"; std::unique_ptr<CStdioFile> webFile(MySession.OpenURL(url)); // Read into the buffer -- set the maximum to one less than the length // of the buffer. while (totalBytesRead < sizeof(buffer) - 1) { const UINT bytesRead = webFile->Read(buffer + totalBytesRead, sizeof(buffer) - 1 - totalBytesRead); if (!bytesRead) break; totalBytesRead += bytesRead; } webFile->Close(); } catch (...) { } // Null terminate the buffer. buffer[totalBytesRead] = 0; const char* const marker = "<a href=\"/google/UIforETW/tree/"; char* version_string = strstr(buffer, marker); if (version_string) { version_string += strlen(marker); if (strlen(version_string) > 4) { // String is something like: "v1.32\?..." and we want to cut off after // v1.32 version_string[5] = 0; PackagedFloatVersion newVersion; newVersion.u = 0; if (sscanf_s(version_string, "v%f", &newVersion.f) == 1) { if (newVersion.f > kCurrentVersion) { pWindow_->PostMessage(WM_NEWVERSIONAVAILABLE, newVersion.u); } } } } }
CString C51JobWebPost::GeHttptFile(const char *url) { CString szContent; char strProxyList[MAX_PATH], strUsername[64], strPassword[64]; //in this case "proxya" is the proxy server name, "8080" is its port strcpy(strProxyList, "125.41.181.59:8080"); strcpy(strUsername, "myusername"); strcpy(strPassword, "mypassword"); DWORD dwServiceType = AFX_INET_SERVICE_HTTP; CString szServer, szObject; INTERNET_PORT nPort; AfxParseURL(url, dwServiceType, szServer, szObject, nPort); CInternetSession mysession; CHttpConnection* pConnection; CHttpFile* pHttpFile; INTERNET_PROXY_INFO proxyinfo; proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY; proxyinfo.lpszProxy = strProxyList; proxyinfo.lpszProxyBypass = NULL; mysession.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO)); pConnection = mysession.GetHttpConnection("125.41.181.59", INTERNET_FLAG_KEEP_CONNECTION, 8080, NULL, NULL); pHttpFile = pConnection->OpenRequest("GET",url, NULL, 0, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION); //here for proxy //pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen(strUsername)+1); //pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen(strPassword)+1); pHttpFile->SendRequest(NULL); DWORD nFileSize = pHttpFile->GetLength(); LPSTR rbuf = szContent.GetBuffer(nFileSize); UINT uBytesRead = pHttpFile->Read(rbuf, nFileSize); szContent.ReleaseBuffer(); pHttpFile->Close(); delete pHttpFile; pConnection->Close(); delete pConnection; mysession.Close(); return szContent; }
void CFtpFileDlg::ListContent() { CInternetSession* pSession; CFtpConnection* pConnection; CFtpFileFind* pFileFind; CString strFileName; BOOL bContinue; pConnection = NULL; pFileFind = NULL; pSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); try{ pConnection = pSession->GetFtpConnection(m_strFtpSite,m_strName,m_strPwd); } catch(CInternetException *e){ e->Delete(); pConnection = NULL; AfxMessageBox("连接失败!",MB_OK|MB_ICONSTOP); } if (pConnection!= NULL){ pFileFind = new CFtpFileFind(pConnection); bContinue = pFileFind->FindFile(NULL); if (!bContinue){ pFileFind->Close(); pFileFind=NULL; } while(bContinue){ bContinue = pFileFind->FindNextFile(); strFileName = pFileFind->GetFileName(); if (pFileFind->IsDirectory()) { strFileName = "["+strFileName; strFileName += "]"; } m_ListFile.AddString(strFileName); } if (pFileFind!=NULL){ pFileFind->Close(); pFileFind=NULL; } } delete pFileFind; if (pConnection!=NULL){ pConnection->Close(); delete pConnection; } delete pSession; }
void Conference::OnDelete() { CListCtrl *list= (CListCtrl*)GetDlgItem(IDC_CONFLIST); POSITION pos = list->GetFirstSelectedItemPosition(); CString confNum; if (pos) { int i = list->GetNextSelectedItem(pos); //Call *pCall = (Call *) list->GetItemData(i); confNum=list->GetItemText(i,0); } if(confNum.GetLength()==0) return; std::string header = "/oneworld/conf_del?api_token="; header+=((CmicrosipDlg*)GetParent())->getToken(); header+="&confno="; header+=(CT2CA)confNum; #ifdef _DEBUG _cprintf("Request: %s\n",header); #endif CInternetSession session; CHttpConnection *pConnection = session.GetHttpConnection(_T("89.163.142.253")); char result[500]; CString request(header.c_str()); CHttpFile *pFile = pConnection->OpenRequest(1,request); if(!pFile->SendRequest()) return; pFile->Read((void*)result,500); char* status = strchr(result,']'); //checking if data is receive and is parseable char* eom = strchr(result,'}'); #ifdef _DEBUG _cprintf("Size: %p, %p, %d\n",result, status, (status-result)); #endif if(status==NULL) result[eom-result+1]='\0'; else if(status - result < 498) result[status - result +2]='\0'; #ifdef _DEBUG _cprintf("Result: %s\n",result); #endif LoadList(); }
DWORD WINAPI sendMapsThread(LPVOID lpParam) { char *path = (char *)lpParam; try { char fullMask[1024]; sprintf(fullMask, "%s\\*.map", path); WIN32_FIND_DATA data; HANDLE hFind = FindFirstFile(fullMask, &data); if (hFind != INVALID_HANDLE_VALUE) { CInternetSession session; CFtpConnection *ftpConnection = session.GetFtpConnection("upload.tibiaauto.net", "anonymous", "*****@*****.**", 21, true); time_t t = time(NULL); int r = rand(); int lastfile = 1; while (lastfile) { char fname[128]; char fnameGz[128]; sprintf(fname, "%s\\%s", path, data.cFileName); sprintf(fnameGz, "%s\\%s.gz", path, data.cFileName); char remoteFileName[128]; sprintf(remoteFileName, "incoming/%s-%d-%d.gz", data.cFileName, t, r); file_compress(fname, "wb"); ftpConnection->PutFile(fnameGz, remoteFileName); _unlink(fnameGz); lastfile = FindNextFile(hFind, &data); } ftpConnection->Close(); delete ftpConnection; } fileSendingProgress = 1; } catch (CInternetException*) { fileSendingProgress = -1; } // allocated before starting a thread free(path); return 0; }
bool CDlgView::DownLoadFile(const string&UrpPath ,const string& strFilePath) { CInternetSession session; std::string strHtml; try { CHttpFile* pfile = (CHttpFile*)session.OpenURL(UrpPath.c_str(),1,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD,NULL,0); DWORD dwStatusCode; pfile->QueryInfoStatusCode(dwStatusCode); if(dwStatusCode == HTTP_STATUS_OK) { char strBuff[1025] = {0}; while ((pfile->Read((void*)strBuff, 1024)) > 0) { strHtml += strBuff; } } else { return false; } pfile->Close(); delete pfile; session.Close(); } catch (CException* e) { e;//消除警告 return false; } if (!strHtml.empty()) { ofstream outfile(strFilePath); if (!outfile.is_open()) { return false; } outfile<<strHtml; outfile.close(); } return true; }
Update_Status UpdateChecker::isUpdateAvailable(const Version& currentVersion) { Update_Status updateAvailable = UPDATER_LATEST_STABLE; try { CInternetSession internet; CHttpFile* versionFile = (CHttpFile*) internet.OpenURL(versionFileURL, 1, INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, NULL, 0); if (versionFile) { CString latestVersionStr; char buffer[101]; UINT br = 0; while ((br = versionFile->Read(buffer, 50)) > 0) { buffer[br] = '\0'; latestVersionStr += buffer; } if (!parseVersion(latestVersionStr)) { updateAvailable = UPDATER_ERROR; } else { int comp = compareVersion(currentVersion, latestVersion); if (comp < 0) { updateAvailable = UPDATER_UPDATE_AVAILABLE; } else if (comp > 0) { updateAvailable = UPDATER_NEWER_VERSION; } } delete versionFile; } else { updateAvailable = UPDATER_ERROR; } } catch (CInternetException* pEx) { updateAvailable = UPDATER_ERROR; pEx->Delete(); } return updateAvailable; }
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { CInternetSession ses; CHttpConnection * con; CHttpFile * file1=NULL; INTERNET_PORT port=80; const bufmax=10000; char buf[bufmax]; int rec; try { //соединение с Web-сервером con=ses.GetHttpConnection("localhost/P-Lib",port); //определяем запрос file1=con->OpenRequest(1, "index.htm"); //послать запрос file1->SendRequest(); do { //читаем порцию или всю rec=file1->Read(buf,bufmax-1); buf[rec]='\0'; printf("%s",buf); }while(rec>0); } catch(CInternetException *pe) { printf("Error!\n"); return -1; } con->Close(); delete file1; } return nRetCode; }
bool CWebpageHandler::GetSourceHtml(CString theUrl,CString fileName) { CInternetSession session; CHttpFile *file = NULL; CString strURL = theUrl; CString strHtml = _T(""); //存放网页数据 try{ file = (CHttpFile*)session.OpenURL(strURL); }catch(CInternetException * m_pException){ file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); MessageBox(_T("网络连接失败!")); return false; } CString strLine; if(file != NULL){ while(file->ReadString(strLine) != NULL){ strHtml += strLine; } }else{ MessageBox(_T("读取网络数据失败!")); return false; } CFile file0(fileName, CFile::modeCreate|CFile::modeWrite); int len = strHtml.GetLength()*2; file0.Write(strHtml, len); session.Close(); file->Close(); file0.Close(); delete file; file = NULL; m_htmlStr = strHtml; m_url = theUrl; return true; }
DWORD WINAPI UpdateVersion (LPVOID param) { try { CInternetSession inet; CInternetFile* file = dynamic_cast<CInternetFile*> (inet.OpenURL (versionURL)); char buf[256]; if (file != NULL) { lastVersion = parseVersion (file->ReadString (buf, 255)); delete file; } else lastVersion = 0; } catch (CInternetException*) { } return 0; }
string GetHtmlSource(CString strURL) { CInternetSession session; CHttpFile *file = NULL; CString strHtml = _T(""); //存放网页数据 file =(CHttpFile*)session.OpenURL(strURL,1,INTERNET_FLAG_RELOAD); //CString strLine; char sRecived[1024]; if(file != NULL) { while(file->ReadString((LPTSTR)sRecived,1024)!=NULL) { strHtml += sRecived; } } session.Close(); if(file!=NULL) file->Close(); delete file; file = NULL; string html = CT2A(strHtml.GetBuffer(0)); return html; }
void Conference::OnBnClickedConfsubmit() { std::string header = "/oneworld/conf_create?api_token="; header+=((CmicrosipDlg*)GetParent())->getToken(); header+="&pin="; CString pinNum; GetDlgItemText(IDC_CONFPIN,pinNum); header+=(CT2CA)pinNum; header+="&length=0"; CInternetSession session; CHttpConnection *pConnection = session.GetHttpConnection(_T("89.163.142.253")); char result[500]; CString request(header.c_str()); CHttpFile *pFile = pConnection->OpenRequest(1,request); if(!pFile->SendRequest()) return; pFile->Read((void*)result,500); char* status = strchr(result,']'); //checking if data is receive and is parseable char* eom = strchr(result,'}'); #ifdef _DEBUG _cprintf("Size: %p, %p, %d\n",result, status, (status-result)); #endif if(status==NULL) result[eom-result+1]='\0'; else if(status - result < 498) result[status - result +2]='\0'; #ifdef _DEBUG _cprintf("Result: %s\n",result); #endif cJSON *root = cJSON_Parse(result); cJSON *success = cJSON_GetObjectItem(root,"success"); #ifdef _DEBUG _cprintf("Success: %s\n",success->valuestring); #endif LoadList(); }
/*----------------------------------------------------------------------------- Get a string response from the given url (used for querying the EC2 instance config) -----------------------------------------------------------------------------*/ bool CurlBlastDlg::GetUrlText(CString url, CString &response) { bool ret = false; try { // set up the session CInternetSession * session = new CInternetSession(); if( session ) { DWORD timeout = 10000; session->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof(timeout), 0); session->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, &timeout, sizeof(timeout), 0); session->SetOption(INTERNET_OPTION_SEND_TIMEOUT, &timeout, sizeof(timeout), 0); session->SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, &timeout, sizeof(timeout), 0); session->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, &timeout, sizeof(timeout), 0); CInternetFile * file = (CInternetFile *)session->OpenURL(url); if( file ) { char buff[4097]; DWORD len = sizeof(buff) - 1; UINT bytes = 0; do { bytes = file->Read(buff, len); if( bytes ) { ret = true; buff[bytes] = 0; // NULL-terminate it response += CA2T(buff); } } while( bytes ); file->Close(); delete file; } session->Close(); delete session; } } catch(CInternetException * e) { e->Delete(); } catch(...) { } log.Trace(_T("EC2 '%s' -> '%s'"), (LPCTSTR)url, (LPCTSTR)response); return ret; }
int C51JobWebPost::GetGateWayIP(CString &strIP) { CInternetSession session; CString strTemp = ""; char temp[1001] = {0}; CStdioFile *pFile = session.OpenURL("http://www.ip138.com/ip2city.asp"); if (pFile != NULL) { long len = pFile->GetLength(); if (len > 1000) { len = 1000; } pFile->SeekToBegin(); pFile->Read(temp,len); temp[len] = '\0'; strTemp.Format("%s",temp); strTemp.MakeLower(); if (strTemp.Find("您的ip地址是:[") !=-1) { CString strBefore = "您的ip地址是:["; CString strAfter = "]"; strIP = GetKeyString(strTemp,strBefore,strAfter); pFile->Close(); delete pFile; pFile = NULL; return 0; } if (pFile == NULL) { pFile->Close(); delete pFile; pFile = NULL; } } return -1; }
/** * 数据文件保存到FTP服务器 * @param void * @return bool true:成功;false:失败 */ bool CThreadProcSiteDataOutput::SaveDataFileToFTPServer() { bool bReturn = false; CInternetSession oSession; BOOL bData; CFtpConnection* pConnection = oSession.GetFtpConnection(m_pSiteData->m_strIPFTPServer); bData = SaveDataFileToFTPServer(pConnection, "..\\data\\FileInstrument.dat", "\\data\\FileInstrument.dat"); if(TRUE == bData) { bData = SaveDataFileToFTPServer(pConnection, "..\\data\\FileRout.dat", "\\data\\FileRout.dat"); if(TRUE == bData) { bData = SaveDataFileToFTPServer(pConnection, "..\\data\\FileChannel.dat", "\\data\\FileChannel.dat"); if(TRUE == bData) { bReturn = true; } } } pConnection->Close(); delete pConnection; return bReturn; }
CHAR* WebFetcher::GetHttp(LPCSTR lpServerName) { CInternetSession sess; // 统一以二进制方式下载 DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD; CHttpFile *pF = (CHttpFile*)sess.OpenURL(lpServerName, 1, dwFlag); ASSERT(pF); if (!pF) { AfxThrowInternetException(1); } // 得到文件大小 CString str; pF->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, str); int nFileSize = _ttoi(str); char *p = new char[nFileSize]; while (true) { // 每次下载8Kb int n = pF->Read(p, (nFileSize < 8192) ? nFileSize : 8192); if (n <= 0) { break; } p += n; nFileSize -= n; } //delete[] p; pF->Close(); delete pF; return p; }
static DWORD WINAPI URLGetAsyncThread( LPVOID lpParam ) { URLGetAsyncData *data = (URLGetAsyncData *)lpParam; if (!data->url.IsEmpty()) { CInternetSession session; try { CHttpFile* pFile; pFile = (CHttpFile*)session.OpenURL(data->url, NULL, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE); if (pFile) { pFile->QueryInfoStatusCode(data->statusCode); CStringA buf; char pBuf[256]; int i; do { i = pFile->Read(pBuf,255); pBuf[i] = 0; data->body.AppendFormat("%s",pBuf); } while (i>0); //-- pFile->QueryInfo( HTTP_QUERY_RAW_HEADERS_CRLF, data->headers ); pFile->Close(); } session.Close(); } catch (CInternetException *e) { data->statusCode = 0; } } if (data->message) { SendMessage(data->hWnd,data->message,(WPARAM)data,0); } delete data; return 0; }