我在使用此代码将数据发送到php服务器时遇到了非常严重的问题。 c ++应用程序发送数据,但是它不是正确地发送数据,而是当我从数据库中查看时它只显示参数中的空字段,可能是什么错误?我的代码就像这样
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <Wininet.h>
#define PAGE_NAME "gate.php"
#pragma comment (lib, "wininet")
void compName()
{
LPCSTR compName = getenv("COMPUTERNAME");
printf("Computer name is : %s\n",compName);
}
void userName()
{
LPCSTR userName = getenv("USERNAME");
printf("UserName is : %s\n",userName);
}
void hWid()
{
HW_PROFILE_INFO hwProfileInfo;
if(GetCurrentHwProfile(&hwProfileInfo))
{
LPCSTR hWid = hwProfileInfo.szHwProfileGuid;
printf("HWID: %s\n",hWid);
}
}
void ip()
{
HINTERNET hInternet , hFile;
DWORD rSize;
char ip[50];
hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
hFile = InternetOpenUrlA(hInternet,"http://housenaija.com/ip/ip.php",NULL,0,INTERNET_FLAG_RELOAD,0);
InternetReadFile(hFile,&ip,sizeof(ip),&rSize);
ip[rSize] ='\0';
InternetCloseHandle(hFile);
InternetCloseHandle(hInternet);
printf("IP adress : %s \n",ip);
}
void sendPCInfo()
{
char hWid[50];
char compName[50];
char userName[50];
char ip[50];
static TCHAR hdrs[] = TEXT("Content-Type: application/x-www-form-urlencoded");
static TCHAR accept[] = "Accept: */*";
char data[1024];
// trying to do a concatenation for gate.php?hWid=&compName=&userName=&ip= to save string to Database
strcpy(data,PAGE_NAME);
lstrcat(data,"?&hwid=");
lstrcat(data,hWid);
lstrcat(data,"&compName=");
lstrcat(data,(const char*)&compName);
lstrcat(data,"&userName=");
lstrcat(data,(const char*)&userName);
lstrcat(data,"&ip=");
lstrcat(data,(const char*)&ip);
//HINTERNET hSession;
//HINTERNET hConnect;
//HINTERNET hRequest;
HINTERNET hSession = InternetOpen("AppName",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
HINTERNET hConnect = InternetConnect(hSession,"localhost",80,NULL,NULL,INTERNET_SERVICE_HTTP ,0,0);
HINTERNET hRequest = HttpOpenRequest(hConnect,"GET","/pcinfo/gate.php",NULL,NULL,NULL,0,0);
HttpSendRequest(hRequest,NULL,0,data,strlen(data));
/*DWORD dwAvailable,dwRead;
char pOutBuf[1024];
char* pTempBuf=pOutBuf;
while(InternetQueryDataAvailable(hRequest,&dwAvailable,0,0) && dwAvailable != 0)
{
DWORD dwRead;
InternetReadFile(hRequest,pTempBuf,dwAvailable,&dwRead);
pTempBuf += dwRead;
}
*/
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
MessageBox(NULL,"PC Information Sent Successfully","Sent Info",MB_ICONINFORMATION | MB_OK);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
compName();
userName();
hWid();
ip();
sendPCInfo();
system("pause");
}
编辑:保存到数据库的服务器端看起来像这样
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "pcinfos";
$con = mysqli_connect($host,$user,$pass,$db) or die ('cannot connect');
mysqli_select_db($con,$db);
$hwid = $_GET['hWid'];
$compName = $_GET['compName'];
$userName = $_GET['userName'];
$ip = $_GET['ip'];
$sql = "INSERT INTO logs (hWid,compName,userName,ip) VALUES ('".$hwid."','".$compName."','".$userName."','".$ip."')";
mysqli_query($con,$sql) or die('Query Failed');
echo "Entry successful";
mysqli_close($con)
?>