c++ - 使用 HttpSendRequestA 发送 POST 失败并显示 ERROR_HTTP_HEADER_NOT_FOUND?
问题描述
我正在尝试.php
在本地计算机上使用 XAMPP 将数据发布到测试脚本,但它总是以ERROR_HTTP_HEADER_NOT_FOUND
. 我检查了服务器日志,没有向 Apache 发送请求。我可以通过网络浏览器手动完成,并且 php 脚本按预期运行。
这是不起作用的 C++ 代码。有人可以指出问题吗?
static const char *accepttypes[]={ "application/x-www-form-urlencoded", NULL};
static const char headers[]="application/x-www-form-urlencoded\r\n";
// open session
HINTERNET hsession;
if ((hsession=InternetOpen(_T("Whatever"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0)) != NULL) {
HINTERNET hconnect;
if ((hconnect=InternetConnect(hsession, _T("127.0.0.1"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL) {
HINTERNET hpostreq;
if ((hpostreq=HttpOpenRequestA(hconnect, "POST", "/mytest.php", NULL, NULL, accepttypes, INTERNET_FLAG_RELOAD, 0)) != NULL) {
BYTE *data=(BYTE*)"xyz=1234";
DWORD datasize=8;
if (HttpSendRequestA(hpostreq, headers, sizeof(headers), (LPVOID) data, datasize)) {
....
// ^^^^^ That always fails with ERROR_HTTP_HEADER_NOT_FOUND
解决方案
我在您的电话中看到两个错误HttpSendRequestA()
:
的值
headers
需要从 更改"application/x-www-form-urlencoded\r\n"
为"Content-Type: application/x-www-form-urlencoded"
。sizeof(headers)
需要更改为-1
,sizeof(headers)-1
或strlen(headers)
. 您应该传入-1
让函数计算headers
字符串的字符长度,或者您可以传入实际的字符长度。字符串的空终止符不包含在该长度中。
推荐阅读
- reactjs - 反应虚拟化和 InfiniteLoader
- javascript - 属性 ref 在 React 中返回 null
- amazon-s3 - Cloudflare-S3 HTTPS 握手
- php - 向谷歌表格添加行时PHP数组奇怪
- watchkit - WatchKit 模态表右上角的“完成”按钮
- r - 来自 r 中不同列的 grep
- python - 如何在 Tensorflow 多 GPU 案例中使用 feed_dict
- android - 将当前值与架构组件中的最后一个值进行比较
- python-3.x - 如何在 kubernetes 上连接到 spark 2.4 时指定要占用的内核数和内存规格?
- python - 排序数据未绘制在正确的数据点中