首页 > 解决方案 > 使用 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

标签: c++winapiwininet

解决方案


我在您的电话中看到两个错误HttpSendRequestA()

  • 的值headers需要从 更改"application/x-www-form-urlencoded\r\n""Content-Type: application/x-www-form-urlencoded"

  • sizeof(headers)需要更改为-1,sizeof(headers)-1strlen(headers). 您应该传入-1让函数计算headers字符串的字符长度,或者您可以传入实际的字符长度。字符串的空终止符不包含在该长度中。


推荐阅读