#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, "POST", "", 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("POST请求响应:", response);
}else{
// 请求失败,处理错误信息
int errorCode = GetLastError();
// 输出错误信息
Print("POST请求失败,错误码:", errorCode);
}
}
MQL4 使用 Windows API 实现 Http POST 和 GET 请求的示例代码
于 2023-06-19 14:11:17 首次发布