URLProcessorRetCode CURLProcessor::OpenURL(CString csURL,BOOL bIsWebSearch) { HINSTANCE hInst; URLProcessorRetCode retCode; retCode = DoOpenURL(csURL,bIsWebSearch); // If we failed to Find existing browser process or DDE failed, // try to run default browser by executing URL document if(retCode != enURLErrorNone && retCode != enURLErrorNoDefaultBrowser) { // TODO - Have Initialize take optional parent parameter hInst = ShellExecute(NULL, "open", m_csURL, NULL, NULL, SW_SHOWDEFAULT); if(hInst <= (HINSTANCE) 32) retCode = enURLErrorNoDefaultBrowser; else retCode = enURLErrorNone; } return retCode; }
DWORD CHttpClient::Request(LPCTSTR lpszURL, CString &strPostData, CFile *pFileSave, CString *pstrResult, PROGRESS_CALLBACK fnCallback, void *cookie ) { DWORD dwRet = HTTP_STATUS_BAD_REQUEST; if( NULL == lpszURL || strlen(lpszURL) == 0 ) return dwRet; int nContentLength = 0; int nContentLengthLocal = 0; int nContentLengthFinished = 0; int nContentLengthTotal = 0; // prepare header CString strHeader; CMapStringToString mapHeader; if( pFileSave && pFileSave->GetPosition() > 0 ) { nContentLengthFinished = (int)pFileSave->GetPosition(); CString strRange; strRange.Format( "bytes=%u-", nContentLengthFinished ); mapHeader.SetAt( szRange, strRange ); } if( pstrResult && pstrResult->GetLength() > 0 ) { nContentLengthFinished = pstrResult->GetLength(); CString strRange; strRange.Format( "bytes=%u-", nContentLengthFinished ); mapHeader.SetAt( szRange, strRange ); } if( m_strCookie.GetLength() > 0 ) mapHeader.SetAt( szCookieKey, m_strCookie ); MakeHttpHeader( mapHeader, strHeader ); // default type and flags DWORD dwHttpRequestFlags = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_EXISTING_CONNECT; // | INTERNET_FLAG_KEEP_CONNECTION; CString strProxy; if( !m_strProxyAddress.IsEmpty() ) strProxy = FormatProxyString( m_nProxyType, m_strProxyAddress, m_nProxyPort ); CInternetSession session( szUserAgentValue, 1, m_nAccessType, strProxy, NULL, INTERNET_FLAG_DONT_CACHE ); // 以下SetOption似乎不起作用 if( !strProxy.IsEmpty() && !m_strProxyAddress.IsEmpty() ) { session.SetOption( INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)m_strProxyUser, m_strProxyUser.GetLength() ); session.SetOption( INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)m_strProxyPasswd, m_strProxyPasswd.GetLength() ); } session.SetOption( INTERNET_OPTION_RECEIVE_TIMEOUT, 300000 ); session.SetOption( INTERNET_OPTION_SEND_TIMEOUT, 30000 ); session.SetOption( INTERNET_OPTION_CONNECT_TIMEOUT, 30000 ); CHttpConnection* pServer = NULL; CHttpFile* pFile = NULL; try { // check to see if this is a reasonable URL DoOpenURL( lpszURL, dwHttpRequestFlags, strHeader, strPostData, &session, &pServer, &pFile, fnCallback, cookie ); if( NULL == pServer || NULL == pFile ) ThrowTearException( ERR_TEAR_INTERRUPTED ); pFile->QueryInfoStatusCode(dwRet); if (dwRet == HTTP_STATUS_MOVED || dwRet == HTTP_STATUS_REDIRECT || dwRet == HTTP_STATUS_REDIRECT_METHOD) { CString strNewLocation = GetNewLocation( pFile ); // close up the redirected site pFile->Close(); delete pFile; pFile = NULL; pServer->Close(); delete pServer; pServer = NULL; // progress callback if( fnCallback ) fnCallback( PROG_REDIRECTING, 0, NULL, cookie ); // open new url DoOpenURL( strNewLocation, dwHttpRequestFlags, strHeader, strPostData, &session, &pServer, &pFile, fnCallback, cookie ); pFile->QueryInfoStatusCode(dwRet); } if (dwRet == HTTP_STATUS_PARTIAL_CONTENT) dwRet = HTTP_STATUS_OK; if (dwRet != HTTP_STATUS_OK) ThrowTearException( ERR_TEAR_INTERRUPTED ); CString strInfo; pFile->QueryInfo( HTTP_QUERY_SET_COOKIE, strInfo ); pFile->QueryInfo( HTTP_QUERY_COOKIE, strInfo ); if( strInfo.GetLength() ) m_strCookie = strInfo; pFile->QueryInfo( HTTP_QUERY_CONTENT_LENGTH, strInfo ); nContentLength = atol( strInfo ); nContentLengthTotal = nContentLength + nContentLengthFinished; if( pstrResult && nContentLengthTotal > 0 ) pstrResult->GetBuffer( nContentLengthTotal+5 ); DWORD dwCheckSum = 0; BOOL bHasCheckSum = FALSE; CString strCheckSum; pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strCheckSum); int nPlace = strCheckSum.Find( szCheckSumKeySuffix ); if ( -1 != nPlace ) { strCheckSum = strCheckSum.Mid( nPlace+strlen(szCheckSumKeySuffix) ); nPlace = strCheckSum.Find( '\n' ); if( nPlace > 0 ) { dwCheckSum = atol( strCheckSum.Left( nPlace ) ); bHasCheckSum = TRUE; } } if( fnCallback ) fnCallback( PROG_TRANSFERRING, 0, NULL, cookie ); DWORD dwCheckSumLocal = 0; TCHAR sz[1028]; int nRead = pFile->Read(sz+4, 1023); while (nRead > 0) { sz[4+nRead] = '\0'; if( NULL != pFileSave ) pFileSave->Write( sz+4, nRead ); if( NULL != pstrResult ) *pstrResult += (TCHAR *)(sz+4); nContentLengthLocal += nRead; if( fnCallback && nContentLengthTotal > 0 ) fnCallback( PROG_PROGRESS, DWORD(STKLIB_MAXF_PROGRESS*(nContentLengthFinished+nContentLengthLocal)/nContentLengthTotal), NULL, cookie ); if( bHasCheckSum ) { *((DWORD *)sz) = dwCheckSumLocal; dwCheckSumLocal = CRC32( sz, nRead ); } nRead = pFile->Read(sz+4, 1023); } if( pstrResult && nContentLengthTotal > 0 ) pstrResult->ReleaseBuffer(); if( (nContentLength > 0 && nContentLengthLocal != nContentLength) || (bHasCheckSum && dwCheckSum != dwCheckSumLocal) ) ThrowTearException( ERR_TEAR_DATATRANSFER ); if( fnCallback ) fnCallback( PROG_PROGRESS, STKLIB_MAX_PROGRESS, NULL, cookie ); } catch (CInternetException* pEx) { // catch errors from WinINet if (HTTP_STATUS_OK == dwRet) dwRet = HTTP_STATUS_PARTIAL; TCHAR szErr[1024]; pEx->GetErrorMessage(szErr, 1024); m_strLastErrorMessage = szErr; pEx->Delete(); } catch (CTearException* pEx) { TCHAR szErr[1024]; pEx->GetErrorMessage(szErr, 1024); m_strLastErrorMessage = szErr; pEx->Delete(); } catch( CException * pEx ) { TCHAR szErr[1024]; pEx->GetErrorMessage(szErr, 1024); m_strLastErrorMessage = szErr; pEx->Delete(); } if (pFile != NULL) { pFile->Close(); delete pFile; } if (pServer != NULL) { pServer->Close(); delete pServer; } session.Close(); if(nContentLength > 0 && nContentLengthLocal != nContentLength) return Request( lpszURL, strPostData, pFileSave, pstrResult, fnCallback, cookie ); return dwRet; }