Handling cookies in Internet functions

Continuing discussion that went off topic from Will InternetPostRequest accepts raw json data?

I checked it. After first login I have to set header "Cookie" (received after logging in) with next post e.g "send trade". Of course, I can use external applications to get this data.

Cookies are completely different story.
Windows Internet (WInet) subsystem handles cookies by itself. You don't need to/should not place them "manually" in the headers. WInet will do that for you.
Also there are specialised functions for cookies that work with WInet database of cookies

When web site responds with cookie, WInet will store that cookie in its own cookie database (shared with Internet Explorer) and will use such cookie automatically in subsequent requests.

1 Like

Hmm, I only know that without send POST without header "Cookie: JSESSIONID=....." request doesn't work :slight_smile:

See the code below. The file on the server sets the cookie in RESPONSE to get request.
The cookie is automatically handled by WInet. Next time you execute code you will get the value of cookie from PREVIOUS execution automatically sent to server in request.
This is all automatic. You don't need to set the cookies yourself. If server sets the cookie in response, it will be automatically sent next time you make HTTP request:

INTERNET_FLAG_RELOAD = 0x80000000;
ih = InternetOpenURL("http://www.amibroker.com/cookie_test.php?my_data=" + Now(), INTERNET_FLAG_RELOAD);

if( ih )
{
  while( ( str = InternetReadString( ih ) ) != "")
    printf("%s", str );

  InternetClose( ih );
}

Server code is as follows (PHP):

<?php
$cookie_name = "data";
$cookie_value = $_GET["my_data"];
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<?php
if(!isset($_COOKIE[$cookie_name])) 
{
  echo "Cookie named '" . $cookie_name . "' is not set!";
} 
else 
{
  echo "Cookie '" . $cookie_name . "' is set!<br>";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
1 Like

A post was merged into an existing topic: Will InternetPostRequest accepts raw json data?

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.