我试图在本地机器上使用XAMPP将数据发布到测试.php
脚本,但使用ERROR_HTTP_HEADER_NOT_FOUND
总是失败。我检查了服务器日志,没有向Apache发送任何请求。我可以通过web浏览器手动完成,php脚本会按预期运行。
以下是不起作用的C++代码。有人能指出问题吗?
static const char *accepttypes[]={ "application/x-www-form-urlencoded", NULL};
static const char headers[]="application/x-www-form-urlencodedrn";
// 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-urlencodedrn"
更改为"Content-Type: application/x-www-form-urlencoded"
。 -
sizeof(headers)
需要更改为-1
、sizeof(headers)-1
或strlen(headers)
。您需要传入-1
,让函数计算headers
字符串的字符长度,也可以传入实际的字符长度。字符串的null终止符不应包含在该长度中。