问题描述
我正在尝试使用本地计算机上的XAMPP将数据发布到测试.PHP
脚本中,但是它总是失败,并显示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,0)) != NULL) {
HINTERNET hconnect;
if ((hconnect=InternetConnect(hsession,_T("127.0.0.1"),INTERNET_DEFAULT_HTTP_PORT,INTERNET_SERVICE_HTTP,0)) != NULL) {
HINTERNET hpostreq;
if ((hpostreq=HttpOpenRequestA(hconnect,"POST","/mytest.PHP",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
字符串的字符长度,或者也可以传入实际的字符长度。字符串的空终止符不应包含在该长度中。