我正在尝试使用Wininet创建一个简单的HTTPRequest,然后在localhost上,它应该通过PHP文件检索信息并保存到磁盘。 wininet
发送没有问题,但我得到空字段作为这样的响应
Info : IP :
我的c ++代码看起来像这样
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <WinInet.h>
#pragma comment (lib, "wininet.lib")
void Request(LPCSTR Host, LPCSTR url, LPCSTR header, LPSTR data, LPSTR ip)
{
HINTERNET internet = InternetOpenA(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET connect = InternetConnectA(internet, Host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET request = HttpOpenRequestA(connect, "POST", url, "HTTP/1.1", NULL, NULL,
INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_NO_UI |
INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_RELOAD, NULL);
int datalen = 0;
if (data != NULL) datalen = strlen(data);
int headerlen = 0;
if (header != NULL) headerlen = strlen(header);
HttpSendRequestA(request, header, headerlen, data, datalen);
InternetCloseHandle(request);
InternetCloseHandle(connect);
InternetCloseHandle(internet);
}
int main()
{
HINTERNET hInternet, hFile;
DWORD rSize;
static char ipa[50];
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hFile = InternetOpenUrlA(hInternet, "https://mytwpc.000webhostapp.com/ip.php", NULL, 0, INTERNET_FLAG_RELOAD, 0);
InternetReadFile(hFile, &ipa, sizeof(ipa) - 1, &rSize);
ipa[rSize] = '\0';
InternetCloseHandle(hFile);
InternetCloseHandle(hInternet);
char *buf = "John Oswald";
if (buf)
{
Request("localhost", "/test/freeze.php", "Content-Type: application/x-www-form-urlencoded", buf, ipa);
printf("Transmission Successful\n");
}
return 0;
}
假设在我的localhost上接收它的PHP代码看起来像这样
<?php
$info = $_POST['buf'];
$ip = $_POST['ip'];
$simi = "Info : $info \n IP : $ip";
$fp = fopen('dg.txt','a+');
fwrite($fp,$simi);
fclose($fp);
?>
这是输出
Info :
IP :