下面是我写的一个实现HTTP文件下载的函数,采用直连方式。如何让它支持HTTP代理?是修改修改某些参数?还是干脆采用别的方式?
BOOL HeslNetOper::HttpFileDown(CString& strDownURL,CString& strLocalSavePath)
{
CInternetSession lInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
//1、打开下载文件
CHttpFile* pHttpFile=NULL;
try
{
pHttpFile=(CHttpFile*)lInternetSession.OpenURL(strDownURL); DWORD dwRet;
pHttpFile->QueryInfoStatusCode(dwRet);
}
catch(CInternetException* e)
{
char szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
pHttpFile=NULL; return FALSE;
}
//2、保存下载文件
if(pHttpFile)
{
//打开本地文件
CStdioFile lStdioFile;
lStdioFile.Open(strLocalSavePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
//向本地文件中写入数据
char chrBuffer[2048];
DWORD dwBytesRead;
while((dwBytesRead=pHttpFile->Read(chrBuffer,2048))>0)
{
lStdioFile.Write(chrBuffer,dwBytesRead);
} //资源回收
lStdioFile.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpFile=NULL;
}
}
BOOL HeslNetOper::HttpFileDown(CString& strDownURL,CString& strLocalSavePath)
{
CInternetSession lInternetSession(NULL,1,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
//1、打开下载文件
CHttpFile* pHttpFile=NULL;
try
{
pHttpFile=(CHttpFile*)lInternetSession.OpenURL(strDownURL); DWORD dwRet;
pHttpFile->QueryInfoStatusCode(dwRet);
}
catch(CInternetException* e)
{
char szCause[255];
e->GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
e->Delete();
pHttpFile=NULL; return FALSE;
}
//2、保存下载文件
if(pHttpFile)
{
//打开本地文件
CStdioFile lStdioFile;
lStdioFile.Open(strLocalSavePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
//向本地文件中写入数据
char chrBuffer[2048];
DWORD dwBytesRead;
while((dwBytesRead=pHttpFile->Read(chrBuffer,2048))>0)
{
lStdioFile.Write(chrBuffer,dwBytesRead);
} //资源回收
lStdioFile.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpFile=NULL;
}
}
解决方案 »
- 如何编写跨平台的C++代码
- 请教一个关于位图颜色和调色板关联的问题
- 怎么用VC 修改/删除 webbrowser控件中 当前网页的cookie
- 如何实现基于对话框程序界面在不同显示器下,界面都是正常的显示的?!谢谢!!!
- 华为4面全部通过了,不过谁能介绍一下中研软件部和固网软件部哪个更有发展前途啊。
- 急!!!如何将ULONG转换为char*??
- ==急用。怎样禁止 PropertySheet 的“帮助”按钮?==
- 大侠帮忙
- 如何实现在对话框中,静态文字能做成超链连接
- 新手求助,列表内容显示到编辑框
- 为什么我写的save as对话框不能自动填上扩展名?
- 一个LINK 2001 和 FATIL ERROR LNK1120 错误,在线等,立马给分!!1
{
char szTextBuffer[1024];
memset(szTextBuffer,0,sizeof(szTextBuffer)); CInternetSession NetSession;
CHttpFile *pFile = NULL;
DWORD dwReadBytes = 0;
TRY
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nServerPort;
if(!AfxParseURL(strUrl,dwServiceType,strServer,strObject,nServerPort))
{
return 1;
//URL address is invalid.
}
CHttpConnection *pHttpConnection = NetSession.GetHttpConnection(strServer,nServerPort,NULL,NULL);
if(pHttpConnection == NULL) //Network net connection.
{
return 5;
}
RE_SEND_HTTP_REQUEST: pFile = pHttpConnection->OpenRequest(
NULL, strObject, NULL, 1, NULL, NULL,
INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION
);
if(pFile==NULL)
{
NetSession.Close();
return 10;
}
//AfxMessageBox("SendRequest");
if(!pFile->SendRequest(NULL))
{
NetSession.Close();
return 15;
}
DWORD dwStatusCode;
if(!pFile->QueryInfoStatusCode(dwStatusCode))
{
NetSession.Close();
return 20;
} if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ || dwStatusCode == HTTP_STATUS_DENIED)
{
char szData[51];
DWORD dwSize;
//may read error info of the file to szData
do
{
//::InternetReadFile(m_hHttpFile, (LPVOID)szData, 50, &dwSize);
dwSize = pFile->Read(&szData,50);
}
while (dwSize != 0);
if(!pFile->ErrorDlg(
NULL,ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA|
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
NULL)
)
{
NetSession.Close();
return 25;
} //until dwStatusCode==HTTP_STATUS_OK we can do next
goto RE_SEND_HTTP_REQUEST;
}
else
{
if(dwStatusCode!=HTTP_STATUS_OK)
{
//NetSession.Close();
//return 30;
}
} //?áè?êy?Yμ?szTextBuffer
DWORD dwLength = pFile->GetLength();
if(dwLength < sizeof(szTextBuffer))
{
dwReadBytes = pFile->Read(szTextBuffer, dwLength);
}
else
{
dwReadBytes = pFile->Read(szTextBuffer, sizeof(szTextBuffer)-1); //?á·μ??μ??á1?
}
strFileText = szTextBuffer;
pFile->Close();
NetSession.Close();
}
CATCH(CInternetException , pEr)
{
//pEr->ReportError();
return 2;
}
CATCH(CFileException , pEr)
{
//pEr->ReportError();
return 3;
}
END_CATCH return 0;
}