0

I'm not very good in C++, you if you see something in the code fragment which could be better, please educate me!

I'm implementing winhttp in an asynchronous fashion. But im having trouble retrieving the response. I cant figure it out. Because you should be able to parsethe whole response at once. Since multiple concurent request can occur, buffering the response (headers+body) in a global variable is not the way to go.

How can I retrieve the response of the http get request? Or else, is it an good practice to execute winhttp synchronous on a new thread (so the main loop doesn;t get blocked and then calls a function when done?):

     void __stdcall cb(HINTERNET h, DWORD_PTR d, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength){ 
         char* s=new char[1];

             DWORD dwSize = 0;
             if (dwInternetStatus==WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE){
                                 MessageBoxA(0,s,"",0);

                WinHttpQueryDataAvailable( h, &dwSize);
                .....

             }

 }

And the call in the main:

...winhttpopen...
WinHttpSetStatusCallback(request, (WINHTTP_STATUS_CALLBACK)whCallback,WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,0);
...winhttpsend....
Roman R.
  • 68,205
  • 6
  • 94
  • 158
Friso Kluitenberg
  • 1,157
  • 1
  • 14
  • 34

1 Answers1

4

Check this sample code on MSDN - Asynchronous Completion in WinHTTP.

The call to WinHttpQueryDataAvailable in QueryData generates a status callback with a WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE completion in the dwInternetStatus parameter. By checking the value pointed to by the lpvStatusInformation parameter, the callback can determine how much data is left to be read, and if there is no remaining data, can proceed to display all the data that has been read.

This shows you that your callback is called with buffer pointer and length of data in it.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • If you have experience with winhttp and can give a hand :-) http://stackoverflow.com/questions/11577503/winhttp-used-in-async-mode-error-internet-cannot-connect-how-to-cleanly-close – Ghita Jul 23 '12 at 10:14
  • 2
    It's funny how the **Asynchronous Completion in WinHTTP** documentation give an example of everything - except the asynchronous callback. – Ian Boyd Feb 21 '17 at 16:10