I am trying to write a cookie from .exe
app and then read it from JavaScript. is it possible?
exe coode:
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetCookie(string url, string name, string data);
static void Main(string[] args)
{
InternetSetCookie("http://www.example.com", "CookieName", "data=cookieData; expires = Sat,01-Jan-2012 00:00:00 GMT; path=/");
}
and JavaScript code:
alert(getCookie("CookieName"));
this prints:
undefined
where get cookie function is:
function getCookie(Name) {
var search = Name + "=";
if (document.cookie.length > 0) { // if there are any cookies
var offset = document.cookie.indexOf(search);
if (offset != -1) { // if cookie exists
offset += search.length;
// set index of beginning of value
var end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(offset, end));
}
}
}