Forex programozás › Fórum › Kérdések az MQL4 programozási nyelvvel kapcsolatban › Kommunikáció webszerverrel › Válasz erre: Kommunikáció webszerverrel
A 600-as build óta a fent linkelt megoldás nem működik, de találtam egy másikat, gondoltam megosztom.
http://www.mql5.com/en/code/11119
A használata csak ennyi:
string result = httpGET("http://somethingsomethingsomething.com/hm.php");
Comment(result);
Hasznos információk:
– érdemes megcsinálni a két apró módosítást, amit az mql fórum harmadik commentje ír
– ha UTF-8-ban van a PHP-d, akkor BOM nélkül töltsd fel, különben az eredmény beolvasásakor találsz néhány karakternyi szemetet a string előtt
– ha mindenképpen ékezetes karaktereket akarsz beolvasni, akkor használj ASCII kódolást
– amikor futtatod a programod, ne felejtsd el engedélyezni a külső DLL-ek használatát
Itt a javított kód annak, aki nem tudja hol kell módosítani.
//+----------------------------------------------------------------------------+
//| mql4-http.mqh |
//+----------------------------------------------------------------------------+
//| Built by Sergey Lukin |
//| contact@sergeylukin.com |
//| |
//| This libarry is highly based on following: |
//| |
//| - HTTP Wininet sample: http://codebase.mql4.com/8115 |
//| - EasyXML parser: http://www.mql5.com/code/1998 |
//| |
//+----------------------------------------------------------------------------+
#import "wininet.dll"
#define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100 // Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy.
#define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000 // Does not add the returned entity to the cache.
#define INTERNET_FLAG_RELOAD 0x80000000 // Forces a download of the requested file, object, or directory listing from the origin server, not from the cache.
int InternetOpenW(
string sAgent,
int lAccessType,
string sProxyName="",
string sProxyBypass="",
int lFlags=0
);
int InternetOpenUrlW(
int hInternetSession,
string sUrl,
string sHeaders="",
int lHeadersLength=0,
int lFlags=0,
int lContext=0
);
int InternetReadFile(
int hFile,
uchar & sBuffer[],
int lNumBytesToRead,
int& lNumberOfBytesRead
);
int InternetCloseHandle(
int hInet
);
#import
int hSession_IEType;
int hSession_Direct;
int Internet_Open_Type_Preconfig = 0;
int Internet_Open_Type_Direct = 1;
int hSession(bool Direct)
{
string InternetAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)";
if (Direct)
{
if (hSession_Direct == 0)
{
hSession_Direct = InternetOpenW(InternetAgent, Internet_Open_Type_Direct, "0", "0", 0);
}
return(hSession_Direct);
}
else
{
if (hSession_IEType == 0)
{
hSession_IEType = InternetOpenW(InternetAgent, Internet_Open_Type_Preconfig, "0", "0", 0);
}
return(hSession_IEType);
}
}
string httpGET(string strUrl)
{
int handler = hSession(false);
int response = InternetOpenUrlW(handler, strUrl,"",0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD);
if (response == 0)
return(false);
uchar ch[100]; string toStr=""; int dwBytes, h=-1;
while(InternetReadFile(response, ch, 100, dwBytes))
{
if (dwBytes<=0) break; toStr=CharArrayToString(ch, 0, dwBytes);
}
InternetCloseHandle(response);
return toStr;
}