Jump to content

Asynchronous http connection


Guest prathima

Recommended Posts

Guest prathima

Hi everybody,

I am trying to download a webpage/file using asynchronous wininet APIs.

Here is my code...

CString url = _T("");

edurl.GetWindowTextW(url);

HINTERNET hInternet, hConnect, hRequest;

hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

hRequestOpenedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

hReceivedResponseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

hInternet = InternetOpen(L"", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);

if (hInternet == NULL)

{

AfxMessageBox(_T("InternetOpen failed, error"));

GetLastError();

CloseHandle(hInternet);

return;

}

// Setup callback function

if (InternetSetStatusCallback(hInternet,

InternetCallback) == INTERNET_INVALID_STATUS_CALLBACK)

{

AfxMessageBox(_T("InternetSetStatusCallback failed, error"));

GetLastError();

CloseHandle(hInternet);

return;

}

hConnect = InternetConnect(hInternet,url,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERV

CE_HTTP,0, 1);

if (hConnect == NULL)

{

if (GetLastError() != ERROR_IO_PENDING)

{

AfxMessageBox(_T("InternetConnect failed, error"));

GetLastError();

CloseHandle(hInternet);

CloseHandle(hConnect);

return;

}

// Wait until we get the connection handle

WaitForSingleObject(hConnectedEvent, INFINITE);

}

DWORD dwOpenRequestFlags;

DWORD dwInfoBufferLength =MAXINFO-1;

BYTE byInfoBuff[10000]="";

// DWORD dwBytesAvailable,dwBytesAvailableFile=0;

// DWORD dwError;

// HANDLE hTempFile;

TCHAR tcVersion[] = L"HTTP/1.1";

TCHAR *pptcAcceptTypes[] ={

L"text/skmobile",

L"text/skive",

L"text/html",

L"text/*",

L"image/gif",

L"image/png",

L"image/x-xbitmap",

L"image/jpeg",

L"*/*",

NULL

};

dwOpenRequestFlags =INTERNET_FLAG_KEEP_CONNECTION

|INTERNET_FLAG_RELOAD ;

hRequest = HttpOpenRequest(hConnect, TEXT("GET"), NULL, (LPCTSTR)tcVersion,NULL,(LPCTSTR * )pptcAcceptTypes, dwOpenRequestFlags, 2);

if (hRequest == NULL)

{

if (GetLastError() != ERROR_IO_PENDING)

{

AfxMessageBox(_T("HttpOpenRequest failed, error"));

GetLastError();

CloseHandle(hInternet);

CloseHandle(hConnect);

CloseHandle(hRequest);

return;

}

// Wait until we get the request handle

WaitForSingleObject(hRequestOpenedEvent, INFINITE);

}

// sending the specified request to the HTTP server

if (!HttpSendRequestEx(hRequest, NULL, 0,NULL,2))

{

if (GetLastError() != ERROR_IO_PENDING)

{

AfxMessageBox(_T("HttpSendRequest failed, error"));

GetLastError();

CloseHandle(hInternet);

CloseHandle(hConnect);

CloseHandle(hRequest);

return;

}

}

WaitForSingleObject(hRequestCompleteEvent, INFINITE);

CHAR buffer[1024];

DWORD dwRead = sizeof(buffer)-1;

INTERNET_BUFFERS ib = { sizeof(INTERNET_BUFFERS) };

ZeroMemory (& ib, sizeof (INTERNET_BUFFERS));

ib.lpvBuffer = buffer;

ib.dwBufferLength = dwRead;

BOOL bOk = InternetReadFileEx( hRequest, &ib, IRF_ASYNC, 2 );

DWORD err = GetLastError();

if(!bOk && GetLastError()==ERROR_IO_PENDING)

{

WaitForSingleObject(hReceivedResponseEvent, INFINITE);

}

while( bOk && ib.dwBufferLength!=0 )

{

m_StrContent += buffer;

bOk = InternetReadFileEx( hRequest, &ib, IRF_ASYNC, 2 );

if(!bOk && GetLastError()==ERROR_IO_PENDING)

{

WaitForSingleObject(hReceivedResponseEvent, INFINITE);

}

}

UpdateData(FALSE);

CloseHandle(hInternet);

CloseHandle(hConnect);

CloseHandle(hRequest);

I am getting error 120 on calling InternetReadFileEx in emulator.

If anybody has any clue on what is the problem in my code plz let me know.

Thanks

Prathima

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.