先看一个简单的GET示例

#include <Windows.h>
#include <winhttp.h>
#include <stdio.h>
int main()
{
HINTERNET sessionHandle = WinHttpOpen(L"WinHttp Example", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, );
if (sessionHandle)
{
HINTERNET connectionHandle = WinHttpConnect(sessionHandle, L"example.com", INTERNET_DEFAULT_HTTP_PORT, );
if (connectionHandle)
{
HINTERNET requestHandle = WinHttpOpenRequest(connectionHandle, L"GET", L"", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, );
if (requestHandle)
{
BOOL success = WinHttpSendRequest(requestHandle, WINHTTP_NO_ADDITIONAL_HEADERS, , WINHTTP_NO_REQUEST_DATA, , , );
if (success)
{
success = WinHttpReceiveResponse(requestHandle, NULL);
if (success)
{
DWORD dwSize;
do
{
dwSize = ;
LPSTR pszOutBuffer;
DWORD dwDownloaded = ;
if (!WinHttpQueryDataAvailable(requestHandle, &dwSize))
{
printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
break;
}
// No more available data.
if (!dwSize)
break;
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + ];
if (!pszOutBuffer)
{
printf("Out of memory\n");
break;
}
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize + );
if (!WinHttpReadData(requestHandle, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
printf("Error %u in WinHttpReadData.\n", GetLastError());
}
else
{
printf("%s", pszOutBuffer);
}
// 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 > );
}
else
{
// Report any errors.
printf("Error %d has occurred.\n", GetLastError());
}
}
else
{
printf("Request failed\n");
}
WinHttpCloseHandle(requestHandle);
}
else
{
printf("Invalid request handle\n");
}
WinHttpCloseHandle(connectionHandle);
}
else
{
printf("Invalid connection handle\n");
}
WinHttpCloseHandle(sessionHandle);
}
else
{
printf("Invalid WinHTTP-session handle\n");
}
system("pause");
return ;
}

有没有一种要崩溃的节奏仅从example.com获取网页代码,就要写约90行的代码

更麻烦的不是代码量的问题,而是这些API暴露了太多的细节(当然,暴露细节的好处是不限制开发员的思想,根据需求灵活编码)。很多时候,我们并不需要考虑那么多的细节

试着封装成C++类的形式,然而我放弃了,真的太麻烦了还是用libcurl或cpr之类的库吧

参考链接:WinHttpReadData function | Microsoft Docs

最新文章

  1. MFC 文件夹选择对话框
  2. Screen Orientation for Windows Phone
  3. Balanced Binary Tree [LeetCode]
  4. 【poj2960】 S-Nim
  5. c++20701除法(刘汝佳1、2册第七章,暴搜解决)
  6. Robot Framework测试框架学习笔记
  7. 在express项目中有效组织和使用mongoose
  8. Qt之保持GUI响应
  9. jsp中的内置对象(9个)、作用
  10. Spring 中JCA CCI分析--转载
  11. Robots协议具体解释
  12. Android项目----AsyncTask异步操作
  13. redis数据类型:Strings
  14. 分布式锁与实现(一)——基于Redis实现
  15. 【Ubuntu 16】安装nginx
  16. [HAOI2008]糖果传递
  17. Java自定义类加载和ClassPath类加载器
  18. 如何迁移完整SQL数据库到另外一台服务器
  19. wordpress如何屏蔽wp-json(禁用REST API)
  20. 89. a^b【快速幂模板】

热门文章

  1. QQ客服代码,支持临时会话
  2. hdu 5461(分类讨论)
  3. android 扩大view的响应区域
  4. AtCoder - 2581 Meaningful Mean
  5. K&amp;R——第五章 指针与数组
  6. 匿名块的四个类型(type rowtype record table)
  7. eclipse中通过search打开第二个文件时 第一个文件就自己关闭了
  8. iOS -- SKKeyframeSequence类
  9. mysql 安装配置及经常使用操作
  10. DELPHI的BPL使用