CInternetSession 错误
场景:CInternetSession下载中的错误判断
CInternetSession下载中的异常判断?
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
DWORD dwType;
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);
int res = FALSE;
res = pHttpFile->SendRequest(); }
这里可以通过GetHttpConnection或者OpenRequest的返回值什么的来判断这个URL是否可用吗? 因为我在程序中,使用这段代码来下载图片,如果图片的URL是错误的或者打不开,在SendRequest()处就会给异常或者直接 跳出程序了,就想早一些判断这个URL是否有效,如果无效就不SendRequest();
------解决方案--------------------
CFile* httpFile = (CFile*)session.OpenURL(strUrl); //打开URL
if (httpFile!=INVALID_HANDLE_VALUE) //URL有效
{
.....
}
------解决方案--------------------
CInternetSession下载中的异常判断?
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
DWORD dwType;
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);
int res = FALSE;
res = pHttpFile->SendRequest(); }
这里可以通过GetHttpConnection或者OpenRequest的返回值什么的来判断这个URL是否可用吗? 因为我在程序中,使用这段代码来下载图片,如果图片的URL是错误的或者打不开,在SendRequest()处就会给异常或者直接 跳出程序了,就想早一些判断这个URL是否有效,如果无效就不SendRequest();
------解决方案--------------------
CFile* httpFile = (CFile*)session.OpenURL(strUrl); //打开URL
if (httpFile!=INVALID_HANDLE_VALUE) //URL有效
{
.....
}
------解决方案--------------------
- C/C++ code
try { CInternetSession session(_T("session")); CHttpFile* pFile = session.OpenURL(...); .... pFile->Close(); sess.Close(); } catch(CInternetException* e) { e->ReportError(); e->Delete(); }