找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 15|回复: 0

MQL4 使用 Windows API 实现 Http POST 和 GET 请求的示例代码

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-25 05:29:00 | 显示全部楼层 |阅读模式
#import "wininet.dll"

   int InternetOpenA(string, int, string, string, int);

   int InternetConnectA(int, string, int, string, string, int, int, int);

   int HttpOpenRequestA(int, string, string, string, string, string[], int, int);

   int HttpAddRequestHeadersA(int, string, int, int);

   int HttpSendRequestA(int, string, int, string, int);

   int InternetReadFile(int, string, int, int[]);

   int HttpQueryInfoA(int, int, string, int[], int[]);

   int InternetCloseHandle(int);

   int InternetSetOptionA(int, int, string, int);

   int InternetErrorDlg(int, int, int, string, int);

   int GetLastError();

   int FormatMessageA(int, int, int, int, string, int, int[]);

   int LocalFree(int);

#end


// 定义常量

#define INTERNET_OPEN_TYPE_DIRECT       1

#define INTERNET_SERVICE_HTTP           3

#define INTERNET_FLAG_RELOAD            0x80000000

#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000

#define INTERNET_FLAG_SECURE            0x00800000

#define HTTP_QUERY_STATUS_CODE          19

#define HTTP_QUERY_CONTENT_LENGTH       5

#define HTTP_QUERY_CONTENT_TYPE         1

#define HTTP_QUERY_SET_COOKIE           43

#define HTTP_STATUS_OK                  200

#define HTTP_STATUS_CREATED             201

#define HTTP_STATUS_ACCEPTED            202

#define HTTP_STATUS_NO_CONTENT          204


// 处理HTTP POST请求

string httpPost(string url, string postData, string cookie){

    // 初始化 WinAPI

    int hInternet = InternetOpenA("Mozilla/4.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

    int hConnect = InternetConnectA(hInternet, url, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

    int hRequest = HttpOpenRequestA(hConnect, "OST", "", NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 1);


    // 设置请求头

    string requestHeaders = "Content-Type: application/x-www-form-urlencoded";

    HttpAddRequestHeadersA(hRequest, requestHeaders, StringLen(requestHeaders), HTTP_ADDREQ_FLAG_REPLACE);


    // 设置请求的Cookie信息

    if (!cookie.IsEmpty()) {

        InternetSetOptionA(hRequest, INTERNET_OPTION_COOKIE, cookie, cookie.GetLength());

    }


    // 发送 POST 请求

    HttpSendRequestA(hRequest, postData, StringLen(postData), NULL, 0);

   

    // 获取响应状态码

    int statusCode = 0;

    int statusSize = sizeof(statusCode);

    HttpQueryInfoA(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &statusCode, &statusSize, NULL);

   

    // 读取响应内容

    char buffer[4096];

    int bytesRead = 0;

    string response = "";

    while (InternetReadFile(hRequest, buffer, sizeof(buffer), &bytesRead) && bytesRead != 0){

        response = response + buffer;

        ZeroMemory(buffer, sizeof(buffer));

    }

   

    // 获取响应中的Cookie信息

    char cookieBuffer[4096];

    int cookieSize = sizeof(cookieBuffer);

    HttpQueryInfoA(hRequest, HTTP_QUERY_SET_COOKIE, cookieBuffer, &cookieSize, NULL);

   

    // 关闭 WinAPI

    InternetCloseHandle(hRequest);

    InternetCloseHandle(hConnect);

    InternetCloseHandle(hInternet);


    // 返回响应内容和Cookie信息

    if(statusCode == HTTP_STATUS_OK || statusCode == HTTP_STATUS_CREATED || statusCode == HTTP_STATUS_ACCEPTED || statusCode == HTTP_STATUS_NO_CONTENT) {

        return response;

    } else {

        return "";

    }

}


// 处理HTTP GET请求

string httpGet(string url, string cookie){

    // 初始化 WinAPI

    int hInternet = InternetOpenA("Mozilla/4.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

    int hConnect = InternetConnectA(hInternet, url, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

    int hRequest = HttpOpenRequestA(hConnect, "GET", "", NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 1);


    // 设置请求的Cookie信息

    if (!cookie.IsEmpty()) {

        InternetSetOptionA(hRequest, INTERNET_OPTION_COOKIE, cookie, cookie.GetLength());

    }


    // 发送 GET 请求

    HttpSendRequestA(hRequest, NULL, 0, NULL, 0);

   

    // 获取响应状态码

    int statusCode = 0;

    int statusSize = sizeof(statusCode);

    HttpQueryInfoA(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &statusCode, &statusSize, NULL);

   

    // 读取响应内容

    char buffer[4096];

    int bytesRead = 0;

    string response = "";

    while (InternetReadFile(hRequest, buffer, sizeof(buffer), &bytesRead) && bytesRead != 0){

        response = response + buffer;

        ZeroMemory(buffer, sizeof(buffer));

    }

   

    // 获取响应中的Cookie信息

    char cookieBuffer[4096];

    int cookieSize = sizeof(cookieBuffer);

    HttpQueryInfoA(hRequest, HTTP_QUERY_SET_COOKIE, cookieBuffer, &cookieSize, NULL);

   

    // 关闭 WinAPI

    InternetCloseHandle(hRequest);

    InternetCloseHandle(hConnect);

    InternetCloseHandle(hInternet);


    // 返回响应内容和Cookie信息

    if(statusCode == HTTP_STATUS_OK || statusCode == HTTP_STATUS_CREATED || statusCode == HTTP_STATUS_ACCEPTED || statusCode == HTTP_STATUS_NO_CONTENT) {

        return response;

    } else {

        return "";

    }

}


// 在OnTick函数中调用示例

void OnTick(){

    string url = "https://example.com";

    string postData = "key1=value1&key2=value2";

    string cookie = ""; // 这里填写需要发送的Cookie值

   

    string response = httpPost(url, postData, cookie);

   

    if(response != ""){

        // 处理响应数据

        Print("OST请求响应:", response);

    }else{

        // 请求失败,处理错误信息

        int errorCode = GetLastError();

        // 输出错误信息

        Print("OST请求失败,错误码:", errorCode);

    }

}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-12 06:40 , Processed in 0.105306 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表