使用mql4的简单POST/GET HttpRequest

 

大家好。

我知道这个话题已经讨论过很多次了。但不幸的是,我找到的所有关于这个话题的文章都太复杂了,或者因为太旧而产生错误信息。因此,我无法创建一个简单的POST/GET请求的脚本。

所以,我只是想向服务器发送POST和GET请求。目标文件是一个简单的php文件。

这是我制作的第一个测试脚本,它是基于https://www.mql5.com/en/code。

#import  "Wininet.dll"
   int InternetOpenA(string, int, string, string, int);
   int InternetConnectA(int, string, int, string, string, int, int, int); 
   int InternetOpenUrlA(int, string, string, int, int, int);
   int InternetReadFile(int, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
   int HttpOpenRequestA(int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestA(int, string, int, string, int);
#import
 

int OnInit()
{
   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenA("HTTP_Client_Sample", 1, NULL, NULL, 0); 
   int HttpConnect = InternetConnectA(HttpOpen, "http://localhost/tradex", 7777, NULL, NULL, 3, 0, 1);
   int HttpRequest = HttpOpenRequestA(HttpConnect, "POST", "/index.php", "HTTP/1.1", NULL, acceptTypes, 0, 1);
   string result = HttpSendRequestA(HttpRequest, headers, StringLen(headers), data, StringLen(data));

   int read[1];
   Print("This is the POST result: " + result);

   return(0);
}

不幸的是,我收到的结果是 "0",这意味着请求不成功。但是我找不到原因。

希望你能帮助我。

希望你能帮助我,谢谢。

尊敬的亚历克斯

 

从你所描述的问题的性质来看,我猜你使用的是B600 >。

所以你必须 使用UNICODE 调整 你的代码 ,例如

int InternetOpenW(...

而不是(ANSI)。

int InternetOpenA(...
 
coolex:

大家好。

我知道这个话题已经讨论过很多次了。但不幸的是,我找到的所有关于这个话题的文章都太复杂了,或者因为太旧而产生错误信息。因此,我无法创建一个简单的POST/GET请求的脚本。

所以,我只是想向服务器发送POST和GET请求。目标文件是一个简单的php文件。

这是我制作的第一个测试脚本,它是基于https://www.mql5.com/en/code。

不幸的是,我收到的结果是 "0",这意味着请求不成功。但是我找不到原因。

希望你能帮助我。

希望你能帮助我,谢谢。

尊敬的亚历克斯


https://www.mql5.com/en/forum/149321
 

好的。谢谢你的快速回复。还没有测试过,但能否请您提供一些更多的细节?对不起,请问B600是什么?此外,我在http://msdn.microsoft.com/en-us/library/windows/desktop/aa385098(v=vs.85).aspx 上找不到 "InternetOpenW "或 "InternetOpenA "功能的文档。

这个"...A "和"...W "是怎么来的?

谢谢你的帮助。

 
coolex:

但什么是B600?

版本600及以上(MetaTrader)=构建600及以上=B600 >

coolex

"...A "和"...W "是怎么来的?

我希望你能读懂

在你提供的链接中的同一页说(在底部)。

Unicode和ANSI名称InternetOpenUrlW(Unicode)和InternetOpenUrlA(ANSI)。

 
好吧,我在http://msdn.microsoft.com/en-us/library/windows/desktop/aa385098(v=vs.85).aspx 网站上找不到 "InternetOpenW "或 "InternetOpenA "函数,因为它们不存在,但 "InternetOpenUrlW "和 "InternetOpenUrlA "当然存在。
这就是为什么我要求"...W "和"...A",阅读不是问题:-)。

核心函数是 "HttpOpenRequest",因为在它的帮助下我可以向一个URL发送POST和GET请求。这里是文档:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384233(v=vs.85).aspx
但是我不知道我应该使用 "HttpOpenRequestA "还是 "HttpOpenRequestW",因为当我使用 "HttpOpenRequestW "时,我收到了错误信息:"读取'Wininet.dll'中的0x0000007B的访问违规"。使用 "HttpOpenRequestA",我仍然有和开始一样的问题。

所以,实际上我认为对于POST/GET请求,我根本不需要 "InternetOpenUrlW "函数。

这是目前的代码,仍然不能工作 :-( 。

#import  "Wininet.dll"
   int  InternetOpenW(string, int, string, string, int);
   int  InternetConnectW(int, string, int, string, string, int, int, int); 
   int  InternetOpenUrlW(int, string, string, int, int, int);
   int  InternetCloseHandle(int); 
   int  HttpOpenRequestA (int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestW(int, string, int, string, int);
#import
 

int OnInit()
{
   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenW("HTTP_Client_Sample", 1, NULL, NULL, 0);  
   int HttpConnect = InternetConnectW(HttpOpen, "http://localhost/tradex", 7777, NULL, NULL, 3, 0, 1);
   int HttpRequest = HttpOpenRequestA(HttpConnect, "POST", "/index.php", "HTTP/1.1", NULL, acceptTypes, 0, 1);
   string result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));

   int read[1];
   Print("This is the POST result: " + result);

   return(0);
}
 

在上面 写道,你需要为UNICODE调整你的代码,我只给了你一个例子,你仍然留下了你需要调整 东西。

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

我提前告诉你,这还不是全部,还有更多

 

嗨,qjol。

在我之前的文章中,我写到将 "HttpOpenRequestA"改为 "HttpOpenRequestW"会导致错误"在'Wininet.dll'中读取到0x0000007B的访问违规"。

所有其他的函数 我已经改成了UNICODE。

如果我误解了,我很抱歉,但是对于一个简单的POST/GET请求来说,还有什么遗漏或错误?

 
在这里 看一下
 

嗨,qjol。

谢谢你的链接,这很有帮助。我可以看到,从一个URL中获取数据 似乎是可行的,但我仍然不知道如何POST 数据。你的脚本定义了函数"HttpOpenRequestW",但没有使用它。此外,该脚本只接收数据,这一点做得很好。

但是,我怎么才能发送数据 呢?它应该用"HttpOpenRequestW"来工作,但正如我之前已经发布的那样,我得到了错误信息:"读取'Wininet.dll'中的0x0000007B的访问权限违反",而且 "HttpOpenRequestA "显然也不能工作。

希望你能帮忙。


 

我决定检查你的代码,我把它修好了,但由于结果=false,我检查了一下,我得到了错误代码 12005,根据微软的错误代码列表,它意味着

error_internet_invalid_url

12005

URL是无效的。


  #import  "Wininet.dll"
   int InternetOpenW(string, int, string, string, int);
   int InternetConnectW(int, string, int, string, string, int, int, int); 
   int InternetOpenUrlW(int, string, string, int, int, int);
   int InternetReadFile(int, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
   int HttpOpenRequestW(int, string, string, string, string, string& AcceptTypes[], int, int);
   bool HttpSendRequestW(int, string, int, string, int);
#import
#import "kernel32.dll"
int GetLastError(void);
#import

   string headers = "Content-Type: application/x-www-form-urlencoded";
   string data = "";
   string acceptTypes[1] = {"*/*"};

   int HttpOpen = InternetOpenW("HTTP_Client_Sample", 1, "", "", 0);  
   int HttpConnect = InternetConnectW(HttpOpen, "http://localhost/tradex", 7777, "", "", 3, 0, 1);
   int HttpRequest = HttpOpenRequestW(HttpConnect, "POST", "/index.php", "HTTP/1.1", "", acceptTypes, 0, 1);
   bool result = HttpSendRequestW(HttpRequest, headers, StringLen(headers), data, StringLen(data));
   Alert ("Last MSDN Error =: ", kernel32::GetLastError());
   
   int read[1]; // not used
   Print("This is the POST result: ", result);
   if (HttpOpen > 0)
   InternetCloseHandle(HttpOpen);
   if (HttpRequest > 0)
   InternetCloseHandle(HttpRequest);
   
   return;