首页 技术 正文
技术 2022年11月23日
0 收藏 401 点赞 3,260 浏览 1979 个字

先看一个简单的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 ;
}

有没有一种要崩溃的节奏使用Windows API发送HTTP请求仅从example.com获取网页代码,就要写约90行的代码

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

试着封装成C++类的形式,然而我放弃了,真的太麻烦了使用Windows API发送HTTP请求还是用libcurl或cpr之类的库吧

参考链接:WinHttpReadData function | Microsoft Docs

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727