我正在使用wininet玩HttpRequests。
我尝试创建一个httpRequest来将数据发送到本地Web服务器上的PHP文件。虽然它发送,但它遭受了崩溃沿线。我的C ++代码看起来像这样
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wininet.h>
#define PAGE_NAME "order.php"
#pragma comment (lib, "wininet")
int main()
{
char firstname[50] = "John";
char lastname[50] = "Darwin";
char tel[50] = "08098688603";
char idnumber[50] = "99057151";
char* data = (char*)VirtualAlloc(NULL, 4052, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
//lstrcpy(data, PAGE_NAME);
lstrcat(data, PAGE_NAME);
lstrcat(data, "?&firstname=%s");
lstrcat(data, firstname);
lstrcat(data, "&lastname=%s");
lstrcat(data, lastname);
lstrcat(data, "&tel=%s");
lstrcat(data, tel);
lstrcat(data, "&idnumber=%s");
lstrcat(data, (char*)idnumber);
HINTERNET hSession = InternetOpen("Vox", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL)
MessageBox(NULL, "Bad InternetOpen", "Error", MB_OK);
HINTERNET hConnect = InternetConnect(hSession, "localhost", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL)
MessageBox(NULL, "Bad InternetConect", "Error", MB_OK);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", PAGE_NAME, NULL, NULL, (const char**)"text/*", 0, 1);
if (hRequest == NULL)
MessageBox(NULL, "Bad OpenRequest", "Error", MB_OK);
if (HttpSendRequestA(hRequest, "Content-Type: app1ication/x-www-form-urlencoded", strlen("Content-Type: app1ication/x-www-form-urlencoded"), data, strlen(data)) == NULL)
MessageBox(NULL, "Bad SendRequest", "Error", MB_OK);
else
MessageBox(NULL, "Data transfer Complete", "Sent Info", MB_ICONINFORMATION | MB_OK);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
虽然它正确地将完整信息发送到php文件,但它在行
中崩溃了 HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", PAGE_NAME, NULL, NULL, (const char**)"text/*", 0, 1);
错误
在ConsoleApplication1.exe中的0x7504712A(wininet.dll)抛出异常:0xC0000005:访问冲突读取位置0x74786574。
如果存在此异常的处理程序,则可以安全地继续该程序。
我可能缺少什么,有什么我没有得到正确的吗?
答案 0 :(得分:0)
似乎接受类型数组没有用NULL
- 指针正确终止:
指向以null结尾的字符串数组的指针,指示客户端接受的媒体类型。这是一个例子。
PCTSTR rgpszAcceptTypes[] = {_T(“text/*”), NULL};
无法使用NULL指针正确终止数组将导致崩溃。