DWORD CIVConnection::QueryInfoStatusCode(HINTERNET hFile) { DWORD dwStatusCode = HTTP_STATUS_OK; ASSERT(hFile); TCHAR szBuffer[80]; DWORD dwLen = sizeof (szBuffer); BOOL bRet = HttpQueryInfo(hFile, HTTP_QUERY_STATUS_CODE, szBuffer, &dwLen, NULL); if (bRet) dwStatusCode = (DWORD) _ttol(szBuffer); else AfxThrowInternetException(0); return dwStatusCode; }
void CIVConnection::InitSession() { CString str = (LPCSTR) IDS_INTERNET_AGENT; DWORD dwError = 0; bool bStopped = false; m_csSession.Lock(); DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG; CString strProxy; switch (m_opts.m_nUseProxy) { case 0: dwAccessType = INTERNET_OPEN_TYPE_DIRECT; break; case 1: dwAccessType = INTERNET_OPEN_TYPE_PROXY; strProxy.Format (_T("http=%s:%d"), (LPCTSTR) m_opts.m_strProxy, m_opts.m_nPort ); break; } if (! (bStopped = m_bStopped) ) { m_hSession = ::InternetOpen (str, dwAccessType, strProxy, NULL, 0); if (! m_hSession) dwError = GetLastError(); } m_csSession.Unlock(); if (bStopped) AfxThrowUserException(); if (dwError) AfxThrowInternetException (0, dwError); }
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; }
void CIVConnection::GetHTTPData(LPCTSTR szQuery, CString &rstr) { TRACE (_T("HTTP Request: %s\n"), szQuery); DWORD dwServiceType = 0; CString strServer; CString strObject; INTERNET_PORT nPort=0; bool bFirst = false; HINTERNET hFile = NULL; bool bFailed = false; try { while(1) { if(!m_strHeader.GetLength()) hFile = ::InternetOpenUrl (m_hSession, szQuery, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0 ); else hFile = ::InternetOpenUrl (m_hSession, szQuery, (LPCSTR)m_strHeader, m_strHeader.GetLength(), INTERNET_FLAG_DONT_CACHE, 0 ); if (! hFile) AfxThrowInternetException(0); rstr.Empty(); const int nSizeBuffer = 1024; BYTE pBuffer [nSizeBuffer]; DWORD dwTotalRead = 0; while (true) { DWORD dwRead = 0; BOOL bResult = ::InternetReadFile (hFile, pBuffer, nSizeBuffer, &dwRead); if (! bResult) AfxThrowInternetException(0); if (dwRead ==0) break; DWORD dwOldTotal = dwTotalRead; dwTotalRead += dwRead; LPTSTR szTarget = rstr.GetBuffer(dwTotalRead); szTarget += dwOldTotal; #ifndef _UNICODE memcpy (szTarget, pBuffer, dwRead); #else MultiByteToWideChar (CP_ACP, 0, (LPCSTR) pBuffer, dwRead, szTarget, dwRead); #endif //_UNICODE rstr.ReleaseBuffer(dwTotalRead); } if ( CheckErrorMessage(rstr) ) bFailed = true; else { DWORD dwStatus = QueryInfoStatusCode(hFile); if(HTTP_STATUS_PROXY_AUTH_REQ == dwStatus && !bFirst && m_opts.m_iProxyAuth) { bFirst = true; CString strIn; CString strOut; unsigned nLen = 0; strIn.Format(_T("%s:%s"), (LPCTSTR)m_opts.m_strProxyLogin, (LPCTSTR)m_opts.m_strProxyPassword); encode64((LPCSTR)strIn, strIn.GetLength(), strOut.GetBufferSetLength(200), 190, &nLen); strOut.ReleaseBuffer(); m_strHeader.Format(_T("Proxy-Authorization: Basic %s\r\n"),(LPCTSTR)strOut); InternetCloseHandle(hFile); continue; } if(!m_opts.m_iProxyAuth) m_strHeader = _T(""); if ( HttpError (hFile, dwStatus) ) bFailed = true; } break; } } catch (CInternetException * e ) { bFailed = true; if ( e->m_dwError != ERROR_INTERNET_OPERATION_CANCELLED && e->m_dwError != ERROR_INVALID_HANDLE ) { TCHAR szError [512] = _T("") ; e->GetErrorMessage(szError, sizeof (szError) / sizeof (TCHAR) ); SetError (GetCommonIVError(szError)); } else TRACE (_T("CIVConnection - canceled\n")); e->Delete(); } TRACE (_T("End of HTTP request\n")); if (hFile) ::InternetCloseHandle (hFile); if (bFailed) AfxThrowUserException(); }
CHAR* WebFetcher::GetHttp(LPCSTR lpServerName) { // start download file char *pBuf = NULL ; int nBufLen = 0 ; TRY { // connection CInternetSession sess ; sess.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT, 30 * 1000) ; sess.SetOption (INTERNET_OPTION_CONNECT_BACKOFF, 1000) ; sess.SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1) ; 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);} // connection status CString str ; pF->QueryInfo (HTTP_QUERY_STATUS_CODE, str) ; // Proxy Authentication Required if (str == _T("407")) { CString strUsername, strPassword ; // pPara->pThis->df_Notify (GetMsg_ProxyValidate(), &WM_DLF_PROXY_VALIDATE(&lpServerName, &strUsername, &strPassword)) ; pF->SetOption (INTERNET_OPTION_PROXY_USERNAME, (VOID*)(LPCTSTR)strUsername, strUsername.GetLength()) ; pF->SetOption (INTERNET_OPTION_PROXY_PASSWORD, (VOID*)(LPCTSTR)strPassword, strPassword.GetLength()) ; pF->SendRequest (NULL) ; } pF->QueryInfo (HTTP_QUERY_STATUS_CODE, str) ; if (str != _T("200")) { pF->Close() ; delete pF ; AfxThrowInternetException(1); } // confirm update pF->QueryInfo (HTTP_QUERY_LAST_MODIFIED, str) ; /* if (!pPara->pThis->df_Notify (GetMsg_CheckTime(), &WM_DLF_CHECKTIME(&lpServerName, &str))) { pF->Close() ; delete pF ; AfxThrowInternetException(1); } */ // start download pF->QueryInfo (HTTP_QUERY_CONTENT_LENGTH, str) ; // file's length // pPara->pThis->df_Notify (GetMsg_StartDownload(), &pPara->strFileURL) ; if (_ttoi(str)) { // know file's size int nLen = (nBufLen = _ttoi(str)) ; char * p = (pBuf = new char[nLen+8]) ; ZeroMemory (p, nLen+8) ; // while (IsWindow(pPara->pThis->GetSafeHwnd())) while (true) { // download 8K every int n = pF->Read (p, (nLen < 8192) ? nLen : 8192) ; if (n <= 0) break ; // success exit p += n ; nLen -= n ; //pPara->pThis->df_Notify (GetMsg_Progress(), &WM_DLF_PROGRESS(&pPara->strFileURL, nBufLen-nLen, nBufLen)) ; } // interrupted if (nLen != 0) { delete[] pBuf; pBuf=NULL; nBufLen = 0 ; } } else { // don't know file's size, save context to a temp file. bstr_t strFile = QueryTempFilePath() ; CFile outFile (strFile, CFile::modeCreate|CFile::modeReadWrite) ; int n = (int)pF->GetLength() ; while (n) { char * pa = new char[n] ; n = pF->Read (pa, n) ; outFile.Write (pa, n) ; // pPara->pThis->df_Notify (GetMsg_Progress(), &WM_DLF_PROGRESS(&pPara->strFileURL, (int)outFile.GetLength(), 0)) ; n = (int)pF->GetLength() ; delete[] pa ; } outFile.Close() ; // success if (n == 0) { DWORD dw ; if (::InternetQueryDataAvailable ((HINTERNET)(*pF), &dw, 0, 0) && (dw == 0)) { LoadFileToBuffer (strFile, pBuf, nBufLen) ; } } ::DeleteFile(strFile) ; } pF->Close() ; delete pF ; } CATCH_ALL(e) {} END_CATCH_ALL /* if (pBuf) { //pPara->pThis->df_Notify (GetMsg_DownFinished(), &WM_DLF_FINISHED(&pPara->strFileURL, pBuf, nBufLen)) ; delete[] pBuf ; } else { //pPara->pThis->df_Notify (GetMsg_Error(), &pPara->strFileURL) ; }*/ //AfxMessageBox(pBuf); return pBuf; //} }