c++ - Win32 应用程序中的 WinHttpReadData 无法正常工作

标签 c++ winapi winhttprequest

我使用 WinHTTP 从 URL http://www.google.com/complete/search?output=toolbar&hl=vi&q=hoazzztf 获取文本.响应文本必须是:<toplevel/>但我得到这样的回应:

enter image description here

有什么想法吗?谢谢!
(响应文本可能包含 UTF-8 字符)

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);

最佳答案

WinHttpReadData 读取原始数据,但您将其强制为 2 字节 WCHAR 的数组。我认为 google 在其页面上使用 utf-8 编码,因此您需要先对其进行转换。

您的代码中的另一个问题是您没有设置终止 0:

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

然后,检查 new 返回的值是没有意义的,因为 new 在失败的情况下抛出异常。

关于c++ - Win32 应用程序中的 WinHttpReadData 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19907557/

相关文章:

c++ - 最小的 GNU Make 构建系统

c - 如何使用文件大小来读取 WinAPI 中的文件?

c# - 从 C# 中的 SetdllDirectory 中指定的路径加载 dll

vba - 使用 VBA 将 Excel xlsm 文件上传到 php 脚本

c++ - 将私有(private)库静态链接到公共(public)库以隐藏符号

c++ - boost::property_tree::ptree 和带有 BOM 的 UTF-8

调用函数FindNextFile抛出访问冲突异常

windows - WinHttpRequest.5.1 是适用于 Windows 11 的良好 API 还是需要 Iexplorer?

python - win32com winhttp post 请求文件

c++ - 什么是 operator class_name() 常量?