How can i send cookie session from WebRequest?

 

Hi,

How can i save the cookie session fro WebRequest function?

I need the cookie session to perform another request .  

 

had you resolve the problem??

 

i have your problem.

 

I start connection with WebRequest but my borker want correct cookies for open order.

 

How can i do this?? 

 

Maybe you should first learn about the HTTP protocol and how it all works. The cookie session (or any other header) can be transferred back and forth via the "headers" and "result_headers" arguments/parameters:

You will have to parse "result_headers" in accordance with the HTTP Headers format in order to extract the cookie information (or other header information you may need). Then when submitting a new request you will have to build the "headers" parameter according to the HTTP rules.

Here are some helpful links to get you started:

Please, note that this is not really a MQL coding related issue and you should look for advice and help about HTTP on other forums dedicated to such matters.

 

i try with result_headers but this is empty...

i send a request and i receve a json file without  HTTP Headers. Is it possibile?

PS: i use a StockPair APIs

 
giannir1988:

i try with result_headers but this is empty...

i send a request and i receve a json file without  HTTP Headers. Is it possibile?

PS: i use a StockPair APIs

No, you have to have something received in the "result_headers" (see below). You must be using the "WebRequest()" function incorrectly, so show your code. How do you expect us to verify your MQL code if you don't present it?

Just replace and use the example request I used here in the code you post, namely: https://www.stockpair.com/session/authenticate?email=john.doe%40example.com&password=APASSWORD&json&wlf&lng=en

I used "Firefox" and "Firebug" and tested the example URL and got the following resulting headers:

HTTP/1.1 200 OK
Date: Mon, 06 Jun 2016 11:53:42 GMT
Server: PWS/8.1.36
Transfer-Encoding: chunked
X-Px: nc h0-s10.p1-mad ( h0-s56.p6-lhr), nc h0-s56.p6-lhr ( origin)
Cache-Control: no-cache
Content-Type: application/json;charset=UTF-8
Content-Encoding: gzip
Vary: Accept-Encoding
Connection: keep-alive
Set-Cookie: LNG=en__COM; Expires=Thu, 04-Jun-2026 11:53:42 GMT; Path=/; HttpOnly

However, we will only be able to verify if you are using the "WebRequest()" function correctly. As for "StockPair APIs" or "HTTP" itself, it is out of the scope of this forum.

 

i undestead that is not the place for explane my doubt, but if you know the way that i can extract a cookie string for my case you make me very happy.

I'm sorry if the argoument is not 100%  pertinent but is a lot difficult post a code that allow me to use this cookie?? 

I receve an html head resultimg heades like you but WHERE are the string cookie???? 

 
giannir1988:

i undestead that is not the place for explane my doubt, but if you know the way that i can extract a cookie string for my case you make me very happy. I'm sorry if the argoument is not 100%  pertinent but is a lot difficult post a code that allow me to use this cookie?? I receve an html head resultimg heades like you but WHERE are the string cookie???? 

I have already provided several links and 2 of those links were specifically about cookies. Please read them and do some research, especially the last link provided as it is an introduction to "Cookies".

If you are not willing to post your own attempt at the example URL I provided for testing purposes, then why should I put any effort into coding an example for you?

We are here to help you with your own code, not to code things for you. If you wish to hire me or another to code it for you, then please place a request on the "Freelance" section.

As for where are the cookies in the "header", it is quite obviously at the line with the "Set-Cookie:" key.

HTTP/1.1 200 OK
...
Set-Cookie: LNG=en__COM; Expires=Thu, 04-Jun-2026 11:53:42 GMT; Path=/; HttpOnly
 

i had extract cookie from html header.

i have this code:

Set-Cookie: JSESSIONID=CDC20F42E880A1C2ECC85E616D8E27F8; Path=/; HttpOnly

Set-Cookie: T=2303315554; Expires=Sun, 10-Jul-2016 07:20:32 GMT; Path=/; HttpOnly

Set-Cookie: LNG=it__COM; Expires=Mon, 08-Jun-2026 07:20:32 GMT; Path=/; HttpOnly

Set-Cookie: BI=4FFE6535815D4DF6BECF98E4EEFFC0C4; Expires=Mon, 08-Jun-2026 07:20:32 GMT; Path=/; HttpOnly

Set-Cookie: BIP=392472465.36895.0000; path=/; HttpOnly

 i replace Set-Cookie with Cookie and have this string:

Cookie: JSESSIONID=CDC20F42E880A1C2ECC85E616D8E27F8; Path=/; HttpOnly

Cookie: T=2303315554; Expires=Sun, 10-Jul-2016 07:20:32 GMT; Path=/; HttpOnly

Cookie: LNG=it__COM; Expires=Mon, 08-Jun-2026 07:20:32 GMT; Path=/; HttpOnly

Cookie: BI=4FFE6535815D4DF6BECF98E4EEFFC0C4; Expires=Mon, 08-Jun-2026 07:20:32 GMT; Path=/; HttpOnly

Cookie: BIP=392472465.36895.0000; path=/; HttpOnly

 in webrequest i use:

 res = WebRequest("POST",command,cookie,NULL,timeout,post,0,result,headers);

is it correct??

 because the broker respond to me that i not is connect :-( 

 
giannir1988:

i had extract cookie from html header.

i have this code:

 i replace Set-Cookie with Cookie and have this string:

 in webrequest i use:

is it correct??

 because the broker respond to me that i not is connect :-( 

No it is wrong! You are only supposed to send the cookie value and nothing else (not the expiration date, path, etc.).

But I have already stated that this is not an MQL4 issue and it is a HTTP issue. It is also dependent on other factors such as the conditions set by the web-service that you may be using.

So, please post in forums about HTTP or direct your queries to the web-service provider in question. This forum is for MQL4.

EDIT: PS! However, as there is one part that is a MQL4 issue:

You are using the first format of WebRequest that is specifically for forms, namely "application/x-www-form-urlencoded". However, I am sure that a web-service will not use that format and you should be using the second format of WebRequest for more generalised flexible interaction. Please refer to the documentation for more details.

int  WebRequest(
   const string      method,           // HTTP method
   const string      url,              // URL
   const string      headers,          // headers 
   int               timeout,          // timeout
   const char        &data[],          // the array of the HTTP message body
   char              &result[],        // an array containing server response data
   string            &result_headers   // headers of server response
   );
Reason: