是否有任何方法手动创建cookie的Internet explorer
InnoSetup - Is there any way to manually create cookie for Internet explorer?
基本上IE记得网站www.stackoverflow.com使用JavaScript cookie,但是否有手动创建一个相同的cookie在InnoSetup代表stackoverflow.com?
Javascript饼干:
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function checkCookie(user) {
var user=getCookie("username");
if (user!="") {
alert("Welcome again " + user);
} else {
user='bandwidth - set to off for example';
setCookie("username",user,30);
}
}
注意:因为没有办法从IE检测我的插件是否安装或没有。我有了一个想法,我必须处理饼干。但是不是IE,我的插件必须在第一次安装时创建该cookie。
编辑:参考http://msdn.microsoft.com/en-us/library/windows/desktop/aa385107%28v=vs.85%29.aspx
如果要创建与指定URL关联的cookie,可以使用InternetSetCookie
函数。要获取指定URL的cookie,可以使用InternetGetCookie
函数。下面是他们的翻译和一个例子,展示了如何创建和读取一个cookie(真正的代码实现讨论错误消息,或包装器函数,我将继续给你;以以下代码为例,展示如何使用这些API函数):
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}'My Program
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
const
ERROR_INSUFFICIENT_BUFFER = 122;
ERROR_NO_MORE_ITEMS = 259;
function InternetSetCookie(lpszUrl: string; lpszCookieName: string;
lpszCookieData: string): BOOL;
external 'InternetSetCookie{#AW}@wininet.dll stdcall';
function InternetGetCookie(lpszUrl: string; lpszCookieName: string;
lpszCookieData: string; var lpdwSize: DWORD): BOOL;
external 'InternetGetCookie{#AW}@wininet.dll stdcall';
function TryCreateCookie(const URL, Name, Data: string): Boolean;
begin
// try to create a specified cookie
Result := InternetSetCookie(URL, Name, Data);
// if the function call failed, we can optionally report the reason why
if not Result then
MsgBox('Cookie creation failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
function TryRetrieveCookie(const URL, Name: string; out Data: string): Boolean;
var
S: string;
BufferSize: DWORD;
begin
// initialize function result
Result := False;
// initialize buffer size to 0 to query needed buffer size
BufferSize := 0;
// first call is to determine whether there's a requested cookie, or if so,
// to retrieve the needed buffer size
if not InternetGetCookie(URL, Name, #0, BufferSize) then
begin
// the function failed as expected, so let's inspect the reason
case DLLGetLastError of
// if the reason for failure was the insufficient buffer, it means that
// there's a cookie matching the request and that we have just received
// the required buffer size
ERROR_INSUFFICIENT_BUFFER:
begin
// initialize buffer size by the previously returned size
SetLength(S, BufferSize div SizeOf(Char));
BufferSize := Length(S);
// and call the function again, now with the initialized buffer; this
// time it should succeed; if it is so, then...
if InternetGetCookie(URL, Name, S, BufferSize) then
begin
// everything went fine, so let's return success state and assign a
// retrieved value to the output parameter
Result := True;
Data := S;
end
else
// the second function call failed; that should not happen...
MsgBox('Cookie retrieval failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
// this code is returned when there's no cookie found
ERROR_NO_MORE_ITEMS:
begin
// no cookie matching the criteria was found; the return value of this
// function has already been initialized to False but it's upon you to
// react on this fact somehow, if needed
end;
else
// the first function call failed for unexpected reason
MsgBox('Cookie search failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
end;
end;
procedure InitializeWizard;
var
CookieData: string;
begin
// try to create cookie
TryCreateCookie('http://example.com', 'MyCookie',
'TestData=1234; expires=Wed,31-Dec-2014 23:59:59 GMT');
// try to retrieve cookie
if TryRetrieveCookie('http://example.com', 'MyCookie', CookieData) then
MsgBox(CookieData, mbInformation, MB_OK);
end;
相关文章:
- 视频HTML没有'无法在Internet Explorer 11上工作
- 在jquery中为显示/隐藏设置cookie
- 按下按钮时保存cookie
- Internet Explorer缺少占位符支持,特别是密码字段
- 如何在Analytics.js中始终了解最新的cookie过期时间
- php httponly cookie在浏览器关闭时被删除
- Internet Explorer中的文本换行错误
- 如何在提交表单时将PHP变量传递到Javascript cookie中
- 为什么我在Internet Explorer上看不到html元素
- CORS-服务器端cookie没有保存在chrome浏览器上
- 如何为后台更改程序代码设置cookie
- 如何从另一个域加载cookie
- 如何在Internet Explorer中使用javascript设置cookie
- Internet Explorer 能否从 Flash 发送 Cookie
- 当脚本从另一个域加载时,无法从 PHP 读取 Internet Explorer 中的 cookie
- 如何在Internet explorer中使用javascript删除不同域名下的cookie文件
- Internet Explorer不允许我使用这个Javascript函数创建会话cookie
- 是否有任何方法手动创建cookie的Internet explorer
- Internet Explorer: cookie的过期日期不起作用
- Internet Explorer not writing to document.cookie?