就是我想用c++的http进行发送数据,比如说像http://127.0.0.1:7999/api/fun1发送数据,应该怎么写我看别人写的使用#include <wininet.h>,#include <json/json.h>这两个,代码
#include <windows.h>
#include <wininet.h>
#include <json/json.h>
#include <iostream>
using namespace Json;
#pragma comment(lib, "wininet.lib")
int main() {
HINTERNET hInternet, hConnect;
DWORD bytesRead;
char buffer[4096];
// 初始化WinINet库
hInternet = InternetOpenA("MyApp", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL) {
std::cerr << "InternetOpenA failed!" << std::endl;
return 1;
}
// 打开HTTP连接
hConnect = InternetOpenUrlA(hInternet, "http://127.0.0.1:7999/api/fun1", NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hConnect == NULL) {
std::cerr << "InternetOpenUrlA failed!" << std::endl;
InternetCloseHandle(hInternet);
return 1;
}
// 构建JSON数据
Json::Value jsonData;
jsonData["key1"] = "value1";
jsonData["key2"] = "value2";
std::string postData = jsonData.toStyledString();
// 发送POST数据
// 发送POST数据
if (!InternetWriteFile(hConnect, postData.c_str(), postData.length(), &bytesRead)) {
DWORD error = GetLastError();
char errorBuffer[4096];
DWORD bufferSize = sizeof(errorBuffer);
if (InternetGetLastResponseInfoA(&error, errorBuffer, &bufferSize)) {
std::cerr << "InternetWriteFile failed! Error: " << error << ", Response: " << errorBuffer << std::endl;
}
else {
std::cerr << "InternetWriteFile failed! Error: " << error << std::endl;
}
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 1;
}
// 读取响应数据
while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
// 在这里处理响应数据,例如打印到控制台
std::cout.write(buffer, bytesRead);
}
// 关闭连接和WinINet库
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return 0;
}
使用这个发送的时候,报错的是InternetWriteFile failed! Error,但是服务器那边接受到了数据,只是没有消息体,并且使用postman是可以发送成功的,应该怎么修改