【问题标题】:InnoSetup - Is there any way to manually create cookie for Internet explorer?InnoSetup - 有没有办法为 Internet Explorer 手动创建 cookie?
【发布时间】:2014-08-31 18:28:23
【问题描述】:

基本上 IE 记住网站 www.stackoverflow.com 使用 JavaScript cookie,但无论如何要手动在 InnoSetup 中代表 stackoverflow.com 创建相同的 cookie?

Javascript cookie:

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

【问题讨论】:

  • 有可能。但你确定这是你想走的路吗?
  • @TLama: 是的 - 这是 InnoSetup 为 IE、Safari 浏览器放置自定义 cookie 的唯一最佳方法,以便下次 IE、Safari 可以帮助 JavaScript 检测。从我们的网站我们提供官方代码签名插件,一旦从浏览器安装插件,我需要检测插件是否安装。 (我尝试了几种方法,但都不是最好的方法)

标签: javascript windows internet-explorer cookies inno-setup


【解决方案1】:

要创建与指定 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;

【讨论】:

  • +1 你是天才大师。非常感谢您,这是一部出色的作品
  • 很高兴我能帮上忙! :-)
  • 这个答案的问题是它在现代 IE 版本中不能正常工作。请参阅此处的 Q10:blogs.msdn.com/b/ieinternals/archive/2009/08/20/…
  • 它有效。使用 innoSetup,您还可以在安装后执行 1)将 IE 启动到执行 JavaScript 并创建 cookie 的静态 URL 2)启动所有浏览器的后端并从 Web 链接创建 cookie
  • 由于我所描述的原因,说“它有效”具有误导性。将 IE 启动到静态 URL 通常会起作用。 “启动所有浏览器的后端”是一个有很多问题的想法。
猜你喜欢
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
相关资源
最近更新 更多