Question

I use WinHTTP to get text from URL http://www.google.com/complete/search?output=toolbar&hl=vi&q=hoazzztf. The reponse text must be: <toplevel/> but I get the reponse like this:

                          enter image description here

Any idea? Thank!
(Reponse text may contains UTF-8 character)

DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
BOOL bResults = FALSE;

  hSession = WinHttpOpen(L"WinHTTP Example/1.0",
      WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
      WINHTTP_NO_PROXY_NAME,
      WINHTTP_NO_PROXY_BYPASS, 0);

  WinHttpSetTimeouts(hSession, 1000, 2000, 1000, 1000);

  // Specify an HTTP server.
  if (hSession)
      hConnect = WinHttpConnect(hSession, L"www.google.com",
      INTERNET_DEFAULT_HTTPS_PORT, 0);

  // Create an HTTP request handle.
  if (hConnect)
      hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/complete/search?output=toolbar&hl=vi&q=hoazzztf",
      NULL, WINHTTP_NO_REFERER,
      WINHTTP_DEFAULT_ACCEPT_TYPES,
      WINHTTP_FLAG_SECURE);

  // Send a request.
  if (hRequest)
      bResults = WinHttpSendRequest(hRequest,
      WINHTTP_NO_ADDITIONAL_HEADERS,
      0, WINHTTP_NO_REQUEST_DATA, 0,
      0, 0);

  if (bResults)
      bResults = WinHttpReceiveResponse(hRequest, NULL);

  if (!bResults)
  {
      SetWindowText(hWnd, L"ERR-4");
      break;
  }

  if (bResults)
  {
      do
      {
          // Check for available data.
          dwSize = 0;
          if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
          {
              break;
          }

          // No more available data.
          if (!dwSize)
              break;

          // Allocate space for the buffer.
          pszOutBuffer = new char[dwSize + 1];
          if (!pszOutBuffer)
          {
              break;
          }

          // Read the Data.
          ZeroMemory((LPVOID)pszOutBuffer, dwSize + 1);

          if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
              dwSize, &dwDownloaded))
          {
          }
          else
          {
              MessageBox(hWnd, (LPCWSTR)pszOutBuffer, L"", NULL);
          }

          // Free the memory allocated to the buffer.
          delete[] pszOutBuffer;

          // This condition should never be reached since WinHttpQueryDataAvailable
          // reported that there are bits to read.
          if (!dwDownloaded)
              break;

      } while (dwSize > 0);
  }


  // Report any errors.

  // Close any open handles.
  if (hRequest) WinHttpCloseHandle(hRequest);
  if (hConnect) WinHttpCloseHandle(hConnect);
  if (hSession) WinHttpCloseHandle(hSession);
Was it helpful?

Solution 2

I fixed:

MessageBox(hWnd, (LPCWSTR)pszOutBuffer, L"", NULL);

to:

int wchars_num = MultiByteToWideChar(CP_UTF8, 0, pszOutBuffer, -1, NULL, 0);
wchar_t* wstr = new wchar_t[wchars_num];
MultiByteToWideChar(CP_UTF8, 0, pszOutBuffer, -1, wstr, wchars_num);

OTHER TIPS

WinHttpReadData reads raw data, but you forced it to be an array of 2-byte WCHAR's. I think google uses utf-8 encoding on their pages, so you need to convert it first.

Another problem in your code is that you don't set terminating 0:

...
else
{
    pszOutBuffer[ dwDownloaded ] = 0;
    MessageBox(hWnd, pszOutBuffer, "", NULL);
}

Then, checking value returned by new is senseless, because new throws an exception in case of failure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top