本文整理汇总了C++中CHttpFile::QueryInfo方法的典型用法代码示例。如果您正苦于以下问题:C++ CHttpFile::QueryInfo方法的具体用法?C++ CHttpFile::QueryInfo怎么用?C++ CHttpFile::QueryInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CHttpFile
的用法示例。
在下文中一共展示了CHttpFile::QueryInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetHTTPS
UINT CCopiagenda::GetHTTPS(CString csURL, CString csHeaders, CString& csRetHeaders, CString& csRetData)
{
UINT nCode;
DWORD dwService;
CString csServer;
CString csPath;
INTERNET_PORT nPort;
CString csUser;
CString csPwd;
AfxParseURLEx(csURL,dwService,csServer,csPath,nPort,csUser,csPwd,0);
CHttpConnection* pConnection = NULL;
pConnection=m_pSession->GetHttpConnection(csServer,INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE,nPort, NULL, NULL);
if(pConnection)
{
BOOL bResult=FALSE;
CHttpFile* pFile = NULL;
pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,csPath,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_SECURE);
if(pFile)
{
try
{
bResult=pFile->SendRequest(csHeaders, NULL, 0);
if(bResult)
{
CString csCode;
pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,csCode);
nCode=atoi(csCode);
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,csRetHeaders);
csRetData=ReadData(pFile);
}
}
catch(CInternetException* e)
{
}
pFile->Close();
delete pFile;
}
pConnection->Close();
delete pConnection;
}
return nCode;
}
示例2: PostHTTP
UINT CMMSSender::PostHTTP(CString csURL, BYTE* strPostData, long lDataSize, CString csHeaders, CString& csRetHeaders, CString& csRetData)
{
UINT nCode=0;
DWORD dwService;
CString csServer;
CString csPath;
INTERNET_PORT nPort;
CString csUser;
CString csPwd;
AfxParseURLEx(csURL,dwService,csServer,csPath,nPort,csUser,csPwd,0);
CHttpConnection* pConnection = NULL;
pConnection=m_pSession->GetHttpConnection(csServer,0, nPort, NULL, NULL);
if(pConnection)
{
BOOL bResult=FALSE;
CHttpFile* pFile = NULL;
pFile=pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,csPath,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_NO_AUTO_REDIRECT);
if(pFile)
{
try
{
bResult=pFile->SendRequest(csHeaders, strPostData, lDataSize);
if(bResult)
{
CString csCode;
pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,csCode);
nCode=atoi(csCode);
CString csHeaders;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,csRetHeaders);
csRetData=ReadData(pFile);
}
}
catch(CInternetException* e)
{
}
pFile->Close();
delete pFile;
}
pConnection->Close();
delete pConnection;
}
return nCode;
}
示例3: DownloadFile
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;
}
示例4: GetHttp
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;
}
示例5: URLGetAsyncThread
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;
}
示例6: _InitFileList
// 初始化更新列表
BOOL CUpdaterApp::_InitFileList()
{
BOOL bReturn(FALSE);
CString strFileList(m_szServer);
strFileList += _T("/Filelist.txt");
CInternetSession sess;
sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000);
sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);
sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 5000);
sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);
sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5000);
sess.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
try
{
CHttpFile* pHttpFile = (CHttpFile*)sess.OpenURL(strFileList, 1, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_TRANSFER_ASCII, NULL, 0);
if (pHttpFile)
{
DWORD dwErrCode(0);
pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, dwErrCode);
if (dwErrCode >= 200 && dwErrCode < 300)
{
CStringA strLineA;
while (pHttpFile->ReadString((LPTSTR)strLineA.GetBuffer(512), 511))
{
strLineA.ReleaseBuffer(); // MFC-bug
CString strLine(strLineA);
strLine.TrimLeft();
if (strLine.GetLength() > 0 && strLine[0] != _T('\\'))
{
continue;
}
std::vector<CString> vecStrs = Split(strLine, _T("^^^^^^"));
if (vecStrs.size() >= 2)
{
LPUPDATEITEM pUpdateItem = new UPDATEITEM;
ZeroMemory(pUpdateItem, sizeof(*pUpdateItem));
lstrcpyn(pUpdateItem->szFileName, vecStrs[0], MAX_PATH);
lstrcpyn(pUpdateItem->szServerMD5, vecStrs[1], 33);
m_arrUpdate.Add(pUpdateItem);
}
}
pHttpFile->Close();
sess.Close();
bReturn = TRUE;
}
else
{
LOG(_T("网站访问错误码:%d"), dwErrCode);
}
}
}
catch (...)
{
LOG(_T("下载列表异常!"));
}
return bReturn;
}
示例7: main
int main(int argc, char* argv[])
{
ShowBanner();
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("MFC Failed to initialize.\n");
return 1;
}
if (argc < 2 || !ParseOptions(argc, argv) || pszURL == NULL)
ShowUsage();
int nRetCode = 0;
CTearSession session(_T("TEAR - MFC Sample App"), dwAccessType);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
try
{
// check to see if this is a reasonable URL
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
cerr << _T("Error: can only use URLs beginning with http://") << endl;
ThrowTearException(1);
}
if (bProgressMode)
{
cerr << _T("Opening Internet...");
VERIFY(session.EnableStatusCallback(TRUE));
}
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
// if access was denied, prompt the user for the password
if (dwRet == HTTP_STATUS_DENIED)
{
DWORD dwPrompt;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
// if the user cancelled the dialog, bail out
if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
{
cerr << _T("Access denied: Invalid password\n");
ThrowTearException(1);
}
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
// were we redirected?
// these response status codes come from WININET.H
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
cerr << _T("Error: Site redirects with no new location") << endl;
ThrowTearException(2);
}
strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
strNewLocation = strNewLocation.Left(nPlace);
// close up the redirected site
pFile->Close();
delete pFile;
pServer->Close();
//.........这里部分代码省略.........
示例8: GetData
string GetData(const char * url, const string referer)
{
CInternetSession session("Mozilla/4.0");
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10*1000);
CHttpConnection * pServer = NULL;
CHttpFile* pFile = NULL;
bool bProgressMode = false;
bool bStripMode = false;
string line = "";
try {
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(url, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
TRACE( _T("Error: can only use URLs beginning with http://") );
return "request failed - can only use URLs beginning with http://";
}
if (bProgressMode)
{
TRACE("Opening Internet...%s\n", url);
VERIFY(session.EnableStatusCallback(TRUE));
}
pServer = session.GetHttpConnection(strServerName, nPort);
string file = url;
int file_start = -1;
if(file.length() > 8) {
file_start = (int)file.find("//");
file_start = (int)file.find("/", file_start+2);
if(file_start > (int)file.length()) file_start = -1;
}
if(file_start > 0) file = file.substr(file_start);
else file = "";
LPCTSTR ref = NULL;
if(referer != "") ref = referer.c_str();
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,file.c_str(), ref);
SetCookie(&session, url);
pFile->SendRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
// if access was denied, prompt the user for the password
if (dwRet == HTTP_STATUS_DENIED)
{
DWORD dwPrompt;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
// if the user cancelled the diaTRACE, bail out
if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
{
TRACE( _T("Access denied: Invalid password"));
return "request failed - Access denied: Invalid password";
}
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
// were we redirected?
// these response status codes come from WININET.H
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
TRACE( _T("Error: Site redirects with no new location") );
return "request failed - Site redirects with no new location";
}
strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
strNewLocation = strNewLocation.Left(nPlace);
// close up the redirected site
//.........这里部分代码省略.........
示例9: GetHTTPData
int GetHTTPData(const char * url, unsigned short **body, string referer, bool b_post, string post_data)
{
CInternetSession session("Azereus 2.2.0.2");
//DWORD dwValue;
//session.QueryOption(INTERNET_OPTION_CONNECT_TIMEOUT, dwValue);
//TRACE("timeout: %d\n", dwValue );
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10*1000);
CHttpConnection * pServer = NULL;
CHttpFile* pFile = NULL;
bool bProgressMode = false;
bool bStripMode = false;
bool isGzipped = false;
string line = "";
int size = 0;
unsigned short * buf = *body;
try {
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(url, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
//TRACE("Error: can only use URLs beginning with http://\n");
//TRACE("URL: '%s'\n", url);
line = "request failed - can only use URLs beginning with http://";
}
if (bProgressMode)
{
TRACE( _T("Opening Internet..."));
VERIFY(session.EnableStatusCallback(TRUE));
}
pServer = session.GetHttpConnection(strServerName, INTERNET_FLAG_DONT_CACHE, nPort);
string file = url;
int file_start = -1;
if(file.length() > 8) {
file_start = (int)file.find("//");
file_start = (int)file.find("/", file_start+2);
}
if(file_start > 0) file = file.substr(file_start);
else file = "";
if(!b_post) {
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,file.c_str(), referer.c_str());
string header = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
header += "Accept-Language: en-us,en;q=0.5\n\r";
header += "Accept-Encoding: gzip\r\n";
header += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n\r";
pFile->SendRequest(header.c_str(),(DWORD)header.length());
}
else {
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, file.c_str(), referer.c_str());
string header = "Content-Type: application/x-www-form-urlencoded\n\r";
pFile->SendRequest(header.c_str(),(DWORD)header.length(),(LPVOID)post_data.c_str(), (DWORD)post_data.length() );
}
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
// if access was denied, prompt the user for the password
if (dwRet == HTTP_STATUS_DENIED)
{
DWORD dwPrompt;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
// if the user cancelled the diaTRACE, bail out
if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
{
TRACE(_T("Access denied: Invalid password\n"));
line = "request failed - Access denied: Invalid password";
}
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
// were we redirected?
// these response status codes come from WININET.H
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
//.........这里部分代码省略.........
示例10: GetWebFile
//.........这里部分代码省略.........
//CInternetSession flags if we need them
//DWORD dwFlags = INTERNET_FLAG_ASYNC;
DWORD dwFlags = NULL;
//Proxy setting if we need them
LPCTSTR pstrProxyName = NULL;
LPCTSTR pstrProxyBypass = NULL;
CMyInternetSession session(pstrAgent, dwAccessType, pstrProxyName, pstrProxyBypass, dwFlags);
//Set any CInternetSession options we may need
int ntimeOut = 30;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000* ntimeOut);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//Enable or disable status callbacks
session.EnableStatusCallback(TRUE);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
DWORD dwRet;
try {
pServer = session.GetHttpConnection(lpstrServer, usPort,
pstrUserName, pstrPassword);
pFile = pServer->OpenRequest(pstrVerb, strPathName, pstrReferer,
1, &pstrAcceptTypes, pstrVersion, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->AddRequestHeaders("User-Agent: GetWebFile/1.0\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);//Check wininet.h for info
//about the status codes
if (dwRet == HTTP_STATUS_DENIED)
{
return dwRet;
}
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewAddress;
//again check wininet.h for info on the query info codes
//there is alot one can do and re-act to based on these codes
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewAddress);
int nPos = strNewAddress.Find(_T("Location: "));
if (nPos == -1)
{
return 0;
}
strNewAddress = strNewAddress.Mid(nPos + 10);
nPos = strNewAddress.Find('\n');
if (nPos > 0)
strNewAddress = strNewAddress.Left(nPos);
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
示例11: Request
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 );
//.........这里部分代码省略.........
示例12: GetFile
//.........这里部分代码省略.........
//m_pSession->SetStatus("Downloading file...");
DWORD dwCount = 0;
CHttpFile* pFile = NULL;
try
{
pFile = (CHttpFile*) m_pSession->OpenURL(szURL, 1,
INTERNET_FLAG_TRANSFER_BINARY
//| INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT |
| INTERNET_FLAG_DONT_CACHE
//| INTERNET_FLAG_RELOAD
);
}
catch (CInternetException* e)
{
TCHAR szCause[255];
e->GetErrorMessage(szCause, 255);
m_pSession->SetStatus(szCause);
// e->ReportError();
e->Delete();
delete pFile;
pFile = NULL;
return FALSE;
}
COleDateTime startTime = COleDateTime::GetCurrentTime();
LPSTR pBuf = NULL;
if (pFile)
{
pBuf = (LPSTR) ::GlobalAlloc(GMEM_FIXED, BUFFER_SIZE+1);
if (!pBuf)
{
pFile->Close();
delete pFile;
return FALSE;
}
BYTE buffer[BUFFER_SIZE+1];
try {
UINT nRead = 0;
dwCount = 0;
do
{
nRead = pFile->Read(buffer, BUFFER_SIZE);
if (nRead > 0)
{
buffer[nRead] = 0;
// JT origional code works in sample but produces Gibber here...
//LPTSTR ptr = strBuffer.GetBufferSetLength(dwCount + nRead + 1);
//memcpy(ptr+dwCount, buffer, nRead);
dwCount += nRead;
//strBuffer.ReleaseBuffer(dwCount+1);
// My alternate
CString str( (LPCSTR) &buffer, sizeof(buffer) ); // Convert byte array to CString
strBuffer.Append(str); // append CString.
COleDateTimeSpan elapsed = COleDateTime::GetCurrentTime() - startTime;
double dSecs = elapsed.GetTotalSeconds();
if (dSecs > 0.0)
{
m_transferRate = (double)dwCount / 1024.0 / dSecs;
//m_pSession->SetStatus("Read %d bytes (%0.1f Kb/s)",
// dwCount, m_transferRate );
}
else
{
//m_pSession->SetStatus("Read %d bytes", dwCount);
m_transferRate = dwCount;
}
}
}
while (nRead > 0);
}
catch (CFileException *e)
{
TCHAR szCause[255];
e->GetErrorMessage(szCause, 255);
m_ErrorMessage = szCause;
m_pSession->SetStatus(szCause);
//e->ReportError();
e->Delete();
delete pFile;
::GlobalFree(pBuf); // mem leak fix by Niek Albers
return FALSE;
}
pFile->QueryInfoStatusCode(m_infoStatusCode);
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS ,m_rawHeaders);
pFile->Close();
::GlobalFree(pBuf); // mem leak fix by Niek Albers
delete pFile;
}
//m_pSession->SetStatus("");
return TRUE;
}
示例13: Download
BOOL CDlgView::Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
ASSERT(strFileURLInServer != "");
ASSERT(strFileLocalFullPath != "");
CInternetSession session(_T("test"));
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
DWORD dwType;
const int nTimeOut = 2000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重试次数
char* pszBuffer = NULL;
try
{
AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
pHttpConnection = session.GetHttpConnection(strServer, wPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject,NULL,1,NULL,NULL,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD);
if(pHttpFile->SendRequest() == FALSE)
return false;
DWORD dwStateCode;
pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
{
HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
pHttpConnection->Close();
session.Close();
return false;
}
char szInfoBuffer[1000]; //返回消息
DWORD dwFileSize = 0; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL);
dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = 0;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据
while(nRead > 0)
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
dwTotalWrite += dwWrite;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
}
delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
}
else
{
delete[]pszBuffer;
pszBuffer = NULL;
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
}
session.Close();
return false;
}
}
catch(...)
{
delete[]pszBuffer;
pszBuffer = NULL;
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
//.........这里部分代码省略.........
示例14: getFile
bool CVBFile::getFile(TCHAR *url, TCHAR *location)
{
bool bRet = true;
CInternetSession sess ;
CHttpFile *phttpFile;
CString Status;
CFile lfile;
char *buf=NULL;
char *pbuf=NULL;
DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE |
INTERNET_FLAG_RELOAD ;
try
{
//here, should think of proxy SetOption.
//
//INTERNET_OPTION_PROXY
//INTERNET_OPTION_PROXY_PASSWORD
//INTERNET_OPTION_PROXY_USERNAME
phttpFile = (CHttpFile*)sess.OpenURL(url, 1, dwFlag);
VERIFY(phttpFile);
phttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, Status) ;
if (Status != _T("200"))
{
bRet=false;
phttpFile->Close();
delete phttpFile ;
goto lExit;
}
phttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, Status) ; // file's length
long filelen = _ttol(Status);
long totalLen = filelen;
buf = new char[filelen+1];
pbuf = buf;
::ZeroMemory(buf, filelen+1);
while(true)
{
int n = phttpFile->Read (buf, (filelen < 8192) ? filelen : 8192) ;
if (n <= 0)
break ;
buf += n ; filelen -= n ;
}
phttpFile->Close();
delete phttpFile;
lfile.Open(location, CFile::modeCreate | CFile::modeWrite);
lfile.Write((void*)pbuf, totalLen);
lfile.Close();
}
catch(...)
{
bRet = false;
goto lExit;
}
lExit:
delete pbuf;
return bRet;
}
示例15: Download
CString CHttpRequest::Download (CString strURL, int nMethod, CString strHeaders, CString strData,
CString strFilename,
bool* pbCancelDownload, DWORD dwDownloadId, int& nError, int nApproxContentLength,
HWND hwndProgress, HWND hwndStatus, CString strMsg)
{
gl_stkDownloads.push_back (dwDownloadId);
bool bUseFile = !strFilename.IsEmpty ();
strStatusMsg = bUseFile ? strFilename : strMsg;
// show the UI controls
if (dwDownloadId == gl_stkDownloads.back ())
{
if (hwndProgress != NULL)
{
::PostMessage (hwndProgress, PBM_SETPOS, 0, 0);
::ShowWindow (hwndProgress, SW_SHOW);
}
if (hwndStatus != NULL)
::PostMessage (hwndStatus, SB_SETTEXT, 1, (LPARAM) strStatusMsg.GetBuffer (0));
}
/*
CString strDisplayFilename, strDisplay;
if (!strFilename.IsEmpty () && (pStatus != NULL))
{
strcpy (strDisplayFilename.GetBuffer (strFilename.GetLength () + 1), strFilename.GetBuffer (0));
strDisplayFilename.ReleaseBuffer ();
// needs ellipsis?
CRect r;
pStatus->GetWindowRect (&r);
r.right -= 300;
HDC hdc = ::GetWindowDC (pStatus->GetSafeHwnd ());
::DrawText (hdc, strDisplayFilename, -1, &r, DT_PATH_ELLIPSIS | DT_MODIFYSTRING);
::ReleaseDC (pStatus->GetSafeHwnd (), hdc);
}*/
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
LPVOID lpvoid = NULL;
DWORD dwServiceType;
CString strServer, strObject;
INTERNET_PORT nPort;
AfxParseURL (strURL, dwServiceType, strServer, strObject, nPort);
// declare CMyInternetSession object
CInternetSession InternetSession (NULL,
//"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; (R1 1.3))",
1, INTERNET_OPEN_TYPE_PRECONFIG);
CString strRetData;
try
{
// establish the HTTP connection
pHttpConnection = InternetSession.GetHttpConnection (strServer, nPort, "", "");
// open the HTTP request
pHttpFile = pHttpConnection->OpenRequest (nMethod, strObject, NULL, 1, NULL,
NULL, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_TRANSFER_BINARY);
if (pHttpFile->SendRequest (strHeaders, strHeaders.GetLength (),
static_cast<void*>(strData.GetBuffer (0)), strData.GetLength ()))
{
DWORD dwRet;
// query the HTTP status code
pHttpFile->QueryInfoStatusCode (dwRet);
if (dwRet >= 400)
{
nError = dwRet;
gl_stkDownloads.remove (dwDownloadId);
return "";
}
CString strContentLen;
LPSTR lpszResult = NULL;
UINT nRead = 0, nTotalRead = 0;
lpvoid = malloc (HTTPFILE_BUFFLEN);
// memory error
if (!lpvoid)
{
AfxMessageBox("Error allocating memory");
if (pHttpConnection)
{
pHttpConnection->Close();
delete pHttpConnection;
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
}
nError = -1;
gl_stkDownloads.remove (dwDownloadId);
return "";
}
//.........这里部分代码省略.........