MT4测试WebRequest的方法
2025-03-06 本文已影响0人
AI_Finance
void OnStart()
{
string url = "http://127.0.0.1:8000/api/ainance/mt/receive_order"; // 修复 URL 中的冒号
string jsonData = "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}";
// 为 postData 分配足够的空间
char postData[1024];
StringToCharArray(jsonData, postData, 0, StringLen(jsonData), CP_UTF8);
string headers = "Content-Type: application/json";
char result[1024]; // 为 result 分配足够的空间
string resultHeaders;
string cookie = "";
int timeout = 10000;
// 发送请求
int responseCode = WebRequest("POST", url, headers, cookie, timeout, postData, StringLen(jsonData), result, resultHeaders);
if (responseCode == 200)
{
string response = CharArrayToString(result);
Print("请求成功,服务器返回:", response);
}
else
{
int errorCode = GetLastError();
PrintFormat("请求失败!响应代码:%d,错误代码:%d", responseCode, errorCode);
}
}