Send HTTP-Post Request With Meta

 

Hi Guys

I'm Trying To Send HTTP-Post request with metatrader.

i tried to write a dll for this action but it doesn't..

when i call the request(); function in dll, the Script/Indicator Will stop working...

here is the source code

// pasargad.cpp : Defines the initialization routines for the DLL.

//

#include "stdafx.h"

#include "pasargad.h"

#define WIN_OS

#define _DEBUG_PRINT(X) /* X */

#include

#include

#include

#include

#include

#pragma comment( lib, "wsock32.lib" )

#define SEND_RQ(MSG) \

send(sock,MSG,strlen(MSG),0);

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

#define MT4_EXPFUNC __declspec(dllexport)

//

// Note!

//

// If this DLL is dynamically linked against the MFC

// DLLs, any functions exported from this DLL which

// call into MFC must have the AFX_MANAGE_STATE macro

// added at the very beginning of the function.

//

// For example:

//

// extern "C" BOOL PASCAL EXPORT ExportedFunction()

// {

// AFX_MANAGE_STATE(AfxGetStaticModuleState());

// // normal function body here

// }

//

// It is very important that this macro appear in each

// function, prior to any calls into MFC. This means that

// it must appear as the first statement within the

// function, even before any object variable declarations

// as their constructors may generate calls into the MFC

// DLL.

//

// Please see MFC Technical Notes 33 and 58 for additional

// details.

//

/////////////////////////////////////////////////////////////////////////////

// CPasargadApp

BEGIN_MESSAGE_MAP(CPasargadApp, CWinApp)

//{{AFX_MSG_MAP(CPasargadApp)

// NOTE - the ClassWizard will add and remove mapping macros here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CPasargadApp construction

CPasargadApp::CPasargadApp()

{

// TODO: add construction code here,

// Place all significant initialization in InitInstance

}

/////////////////////////////////////////////////////////////////////////////

// The one and only CPasargadApp object

CPasargadApp theApp;

using namespace std;

MT4_EXPFUNC int request (char* hostname, char* api, char* parameters, string& message)

{

WSADATA WsaData;

WSAStartup (0x0101, &WsaData);

sockaddr_in sin;

int sock = socket (AF_INET, SOCK_STREAM, 0);

if (sock == -1) {

return -100;

}

sin.sin_family = AF_INET;

sin.sin_port = htons( (unsigned short)80);

struct hostent * host_addr = gethostbyname(hostname);

if(host_addr==NULL) {

_DEBUG_PRINT( cout<<"Unable to locate host"<<endl );

return -103;

}

sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;

_DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);

if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {

_DEBUG_PRINT( cout<<"connect failed"<<endl ) ;

return -101;

}

string send_str;

SEND_RQ("POST ");

SEND_RQ(api);

SEND_RQ(" HTTP/1.0\r\n");

SEND_RQ("Accept: */*\r\n");

SEND_RQ("User-Agent: Mozilla/4.0\r\n");

char content_header[100];

sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));

SEND_RQ(content_header);

SEND_RQ("Accept-Language: en-us\r\n");

SEND_RQ("Accept-Encoding: gzip, deflate\r\n");

SEND_RQ("Host: ");

SEND_RQ("hostname");

SEND_RQ("\r\n");

SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");

//If you need to send a basic authorization

//string Auth = "username:password";

//Figureout a way to encode test into base64 !

//string AuthInfo = base64_encode(reinterpret_cast(Auth.c_str()),Auth.length());

//string sPassReq = "Authorization: Basic " + AuthInfo;

//SEND_RQ(sPassReq.c_str());

SEND_RQ("\r\n");

SEND_RQ("\r\n");

SEND_RQ(parameters);

SEND_RQ("\r\n");

_DEBUG_PRINT(cout<<"####HEADER####"<<endl);

char c1[1];

int l,line_length;

bool loop = true;

bool bHeader = false;

while(loop) {

l = recv(sock, c1, 1, 0);

if(l<0) loop = false;

if(c1[0]=='\n') {

if(line_length == 0) loop = false;

line_length = 0;

if(message.find("200") != string::npos)

bHeader = true;

}

else if(c1[0]!='\r') line_length++;

_DEBUG_PRINT( cout<<c1[0]);

message += c1[0];

}

message="";

if(bHeader) {

_DEBUG_PRINT( cout<<"####BODY####"<<endl) ;

char p[1024];

while((l = recv(sock,p,1023,0)) > 0) {

_DEBUG_PRINT( cout.write(p,l)) ;

p[l] = '\0';

message += p;

}

_DEBUG_PRINT( cout << message.c_str());

} else {

return -102;

}

WSACleanup( );

return 0;

}

MT4_EXPFUNC int __stdcall Bamdad(int a, int b) {

return a+b;

}

 

AnyOne..?

Anyone can Help ?

 

one of my friends suggested me to use cURL Library ..

i try to but i couldn't i think i have to write a DLL using cURL to send request with a single function and just use that function in meta ...

any suggestion?

 

I just noticed this - you looking for help on this? I have done this with the cURL library.

 

Here is what i use ( Visual Studio 2008 - C++ DLL )

Code:

//---- You need to add these includes at the top of your code

//---- #include

//---- #include

MT4_EXPFUNC int __stdcall Sample( int Whatever )

{

//---- Declare Return Value

CHAR buffer[2048] ;

CString m_strContents;

DWORD dwRead;

/* Connect to the internet */

HINTERNET hiNet = InternetOpen(

L"InetURL/1.0",

INTERNET_OPEN_TYPE_PRECONFIG,

NULL,

NULL,

0

);

/* if connection fails throw error */

if( !hiNet ) { return( -1010 ); }

/* Connect to a site */

HINTERNET hConnection = InternetConnect(

hiNet,

L"www.yoursite.com",

INTERNET_DEFAULT_HTTP_PORT,

NULL,

NULL,

INTERNET_SERVICE_HTTP,

0,

0

);

/* if connetion to site failed */

if( !hConnection )

{

InternetCloseHandle(hiNet);

return( -1020 );

} // COULD NOT CONNECT TO WEBSITE

/* Get Data */

HINTERNET hData = HttpOpenRequest( hConnection, L"GET", L"/yourpage.php", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );

if ( !hData )

{

InternetCloseHandle(hConnection);

InternetCloseHandle(hiNet);

return( -1030 ); // PAGE NOT FOUND

}

HttpSendRequest( hData, NULL, 0, NULL, 0);

bool Done = false;

while( !Done )

{

InternetReadFile( hData, buffer, 255, &dwRead );

if ( dwRead == 0 ) { Done = true; }

buffer[dwRead] = 0;

m_strContents += buffer;

}

//---- Here you can output m_strContents

MessageBox( 0, m_strContents, L"WebPage OutPut", 0 );

InternetCloseHandle(hConnection);

InternetCloseHandle(hiNet);

InternetCloseHandle(hData);

}

[/PHP]

You could use post instead of get and pass variables in your url ...

[PHP]

/* Post Data */

HINTERNET hData = HttpOpenRequest( hConnection, L"POST", L"/yourpage.php?var1=value1&var2=value2 etc ... ", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );

 

Guys, there is much more simple way, you can write your request to file, then throw alert - and in metatrader properties - event tab - choose instead of wav file - your bat file, then construct bat file in the way that will read the requrest from txt file.

Simple and works allways - without any DLL's

 

Thanks, very useful,

just two question

1. and are the one I am getting on Google or should I search them somewhere else ?

2. What if I need to receive a response from my request ?

Thanks a lot !

Reason: