|
运行平台:ARM4;
运行系统:wince5.0
开发工具:EVC4.0(sp4)
做一个在平台上运行的客户端程序,可以有线上网下载东西。
在EVC上采用了CInternetSession, CHttpConnection, CHttpFile类去实现。
代码:
DWORD dwFlags = 0;
InternetGetConnectedState( &dwFlags, 0 );
CInternetSession session( NULL, 1,
( dwFlags & INTERNET_CONNECTION_PROXY ) == INTERNET_CONNECTION_PROXY ?INTERNET_OPEN_TYPE_PRECONFIG :INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
AfxParseURL( m_strDownloadUrl, dwFlags, m_strServer, m_strObject, m_nPort );
try {
if ( dwFlags== AFX_INET_SERVICE_HTTP ) {
m_pFile = NULL;
CString strRangeQuest;
if ( m_bSupportResume )
{
strRangeQuest.Format( _T("%sRange: bytes=%d-%d\r\n"), szHeaders, m_state.range[2*index], m_state.range[2 * index + 1] );
}
else
{
strRangeQuest = szHeaders;
}
try {
m_pFile = (CHttpFile*) session.OpenURL( m_strDownloadUrl, 1, INTERNET_FLAG_TRANSFER_BINARY, strRangeQuest, -1 );
}
catch (CInternetException* e)
{
TCHAR szCause[MAX_PATH] = {0};
e->GetErrorMessage(szCause, MAX_PATH);
AfxMessageBox( szCause );
e->Delete();
delete m_pFile;
m_pFile = NULL;
session.Close();
return -1;
}
if ( m_pFile != NULL ) {
CFile file;
CString name;
name.Format( _T("%d"), index );
name = m_strTempSavePath + name;
int bOpen = 0;
if(!m_bResume)
bOpen = file.Open(name, CFile::modeCreate | CFile::modeWrite);
else
bOpen = file.Open(name, CFile::modeWrite);
if(bOpen == 0){
TRACE(_T("Error in file open!\n"));
m_pFile->Close();
session.Close();
return -10;
}
file.SeekToEnd();
BYTE buffer[BUFFER_SIZE+1] = {0};
try {
UINT nRead = 0;
m_dwDownloadSize[index] = 0;
do {
if ( m_bTerminate[index] ) {
m_state.range[2 * index] += m_dwDownloadSize[index];
m_pFile->Close();
session.Close();
file.Close();
return -5;
}
try {
nRead = m_pFile->Read( buffer, BUFFER_SIZE );
}
catch( CInternetException *pe)
{
TCHAR szCause[MAX_PATH] = {0};
pe->GetErrorMessage(szCause, MAX_PATH);
AfxMessageBox( szCause );
pe->Delete();
m_state.range[2 * index] += m_dwDownloadSize[index];
m_pFile->Close();
session.Close();
file.Close();
return -5;
}
if ( nRead > 0) {
buffer[nRead] = 0;
file.Write( buffer, nRead );
m_dwDownloadSize[index] += nRead;
}
} while ( nRead > 0 );
}
catch (CFileException *e)
{
TCHAR szCause[MAX_PATH] = {0};
e->GetErrorMessage(szCause, MAX_PATH);
AfxMessageBox( szCause );
e->Delete();
m_state.range[2 * index] += m_dwDownloadSize[index];
m_pFile->Close();
session.Close();
file.Close();
return -5;
}
m_pFile->Close();
file.Close();
}
else {
session.Close();
return -1;
}
} else {
AfxMessageBox( _T("only support HTTP file download!") );
session.Close();
return -1;
}
}
catch ( CException* pEx ) {
TCHAR szErrorMsg[MAX_PATH] = {0};
pEx->GetErrorMessage(szErrorMsg, MAX_PATH);
AfxMessageBox( szErrorMsg );
pEx->Delete();
if ( m_pFile )
m_pFile->Close();
session.Close();
return -1;
}
session.Close();
return 0;
这段代码在PC上运行时可以对网络突然断开的问题进行处理,因为系统会把CInternetException类异常抛给try-catch ( CInternetException* pEx )语句处理。但是这段代码在平台上运行,网络突然断开时,系统没有把CInternetException类异常抛给try-catch语句处理,系统似乎认为程序没有语句在catch这个异常。
开始我认为是EVC编译器的问题,去查了资料,微软有出RTTI.exe补丁对有类型的try-catch进行支持,按照微软的步骤做了,但是try-catch语句还是不
能捕获到异常,或者说系统没有把异常抛出给应用程序自己处理。
请问大家在wince5.0上用wininet做网络编程对这些网络异常问题是怎样处理的,似乎只能去catch CInternetException异常,函数一般在异常情况下都不返回。
谢谢。
|
|