daitt
asked on
CInternetSession - invalid handle type
I use CInternetSession to get HTTP files. My application is multi-threaded. The code to get the files is like this:
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
TRY
{
if (mysession == NULL) mysession = &defaultsession;
mysession->SetOption(INTER NET_OPTION _CONNECT_T IMEOUT, 1000 * 20);
mysession->SetOption(INTER NET_OPTION _CONNECT_B ACKOFF, 1000);
mysession->SetOption(INTER NET_OPTION _CONNECT_R ETRIES, 2);
CString s = url;
//CStdioFile *remotefile = mysession.OpenURL(s,1,INTE RNET_FLAG_ TRANSFER_B INARY);
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
const TCHAR szHeaders[] =_T("Accept: */*\r\n");
if (!AfxParseURL(s, dwServiceType, strServerName, strObject, nPort))
{
}
//DWORD time = GetTickCount();
pServer = mysession->GetHttpConnecti on(strServ erName, nPort);
//TRACE("get connection:%d\n",GetTickCo unt()-time );
//time = GetTickCount();
pFile = pServer->OpenRequest(CHttp Connection ::HTTP_VER B_GET, strObject);
pFile->AddRequestHeaders(s zHeaders);
if (!pFile->SendRequest()) {
AfxMessageBox("Failed");
THROW(new CException());
}
//TRACE("Send request:%d\n",GetTickCount ()-time);
//time = GetTickCount();
DWORD dwRet;
pFile->QueryInfoStatusCode (dwRet);
Error occurs at QueryInfoStatusCode call. The stack trace is:
CInternetFile::AssertValid () line 1307 + 25 bytes
CHttpFile::AssertValid() line 2281
AfxAssertValidObject(const CObject * 0x009e31c0 {FILE*=0x00000000 name=???}, const char * 0x5f4cdb80 THIS_FILE, int 2225) line 108
CHttpFile::QueryInfoStatus Code(unsig ned long & 3435973836) line 2226
GetHttpFile(CString {"http://203.239.186.211:8080/webdemo/kyoto/map01.ipq/a1"}, unsigned char * & 0xcccccccc, int & -858993460, CInternetSession * 0x009e5678 {CInternetSession}) line 468
The code of CInternetFile where error occurs is:
if (IsKindOf(RUNTIME_CLASS(CH ttpFile)))
{
ASSERT(dwResult == INTERNET_HANDLE_TYPE_HTTP_ REQUEST);
}
Value of dwResult is 4096 (INTERNET_HANDLE_TYPE_HTTP _REQUEST is 13).
I have one default session for generic requests (single threaded), some other sessions (one at a time) to retrieves many files (multi-threaded). The errors occurs on both types of session.
Any idea to get over this?
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
TRY
{
if (mysession == NULL) mysession = &defaultsession;
mysession->SetOption(INTER
mysession->SetOption(INTER
mysession->SetOption(INTER
CString s = url;
//CStdioFile *remotefile = mysession.OpenURL(s,1,INTE
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
const TCHAR szHeaders[] =_T("Accept: */*\r\n");
if (!AfxParseURL(s, dwServiceType, strServerName, strObject, nPort))
{
}
//DWORD time = GetTickCount();
pServer = mysession->GetHttpConnecti
//TRACE("get connection:%d\n",GetTickCo
//time = GetTickCount();
pFile = pServer->OpenRequest(CHttp
pFile->AddRequestHeaders(s
if (!pFile->SendRequest()) {
AfxMessageBox("Failed");
THROW(new CException());
}
//TRACE("Send request:%d\n",GetTickCount
//time = GetTickCount();
DWORD dwRet;
pFile->QueryInfoStatusCode
Error occurs at QueryInfoStatusCode call. The stack trace is:
CInternetFile::AssertValid
CHttpFile::AssertValid() line 2281
AfxAssertValidObject(const
CHttpFile::QueryInfoStatus
GetHttpFile(CString {"http://203.239.186.211:8080/webdemo/kyoto/map01.ipq/a1"}, unsigned char * & 0xcccccccc, int & -858993460, CInternetSession * 0x009e5678 {CInternetSession}) line 468
The code of CInternetFile where error occurs is:
if (IsKindOf(RUNTIME_CLASS(CH
{
ASSERT(dwResult == INTERNET_HANDLE_TYPE_HTTP_
}
Value of dwResult is 4096 (INTERNET_HANDLE_TYPE_HTTP
I have one default session for generic requests (single threaded), some other sessions (one at a time) to retrieves many files (multi-threaded). The errors occurs on both types of session.
Any idea to get over this?
ASKER
The problem was that multi threads accessing the same internet session may cause problem. I think Internet session is not thread-safe. I create separated internet session for each thread and it's ok now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it seems like OpenRequest return wrong pointer
can you check every WinInet Call result?
You can call GetLastError API to get WinInet call reslts.