【问题标题】:Looping over CFHTTP cookie and issues setting up cookie循环 CFHTTP cookie 和设置 cookie 的问题
【发布时间】:2014-12-08 20:52:47
【问题描述】:

我正在尝试遍历来自 cfhttp 的 cookie,但它没有显示正确的结果。

下面是我的代码

<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false">
  <cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
  <cfloop collection="#cookies#" item="k">
    <cfset temp = REReplace(k, ";.*", "")>
    <cfset cookieName = listfirst(temp,'=')>
    <cfset cookievalue = listlast(temp,'=')>
    <cfhttpparam type="cookie" name="#cookieName#" value="#cookievalue#">
  </cfloop>
  <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
  <cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>

第二次尝试:

从一次通话中,我得到了 cookie,并将它们放入如下结构中:

<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset cookieStruct=StructNew()>
<cfloop collection="#cookies#" item="key">
  <cfset cookieKeyAndValue = REReplace(key, ";.*", "")>
  <cfset cookieKey = listfirst(cookieKeyAndValue,'=')>
  <cfset cookieValue = listlast(cookieKeyAndValue,'=')>
  <cfset StructInsert(cookieStruct,cookieKey,cookieValue)>
</cfloop>
<cfdump var="#cookieStruct#" abort>
<cfhttp url="#addr#" method="get" throwOnError="Yes" resolveurl="false" result="objAddress">
  <cfloop collection="#cookieStruct#" item="key">
    <cfhttpparam type="cookie" name="#key#" value="#cookieStruct[key]#">
  </cfloop>
  <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
  <cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>

这给了我一个错误:

无效集合 ASPJGASGHSG=KBHFPN;路径=/。必须是有效的结构或 COM 对象。循环错误。

【问题讨论】:

  • 你的描述很模糊。实际发生了什么?结果与您的预期有何不同?您采取了哪些调试步骤?
  • (编辑)错误信息看起来很清楚。返回值即 cfhttp.responseHeader['Set-Cookie'] 不是结构。你需要parse the value and create your own structure

标签: coldfusion


【解决方案1】:

CFHTTP GET 将结果返回到 CFHTTP 数据结构中。在 CFHTTP 打开/关闭标签中包含循环会导致尝试循环一些尚不存在的内容。

<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false" >
    <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
    <cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>

<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
    <cfset temp = REReplace(k, ";.*", "")>
    <cfset cookieName = listfirst(temp,'=')>
    <cfset cookievalue = listlast(temp,'=')>
</cfloop>

我不确定您在此处尝试做什么,但如果有多个 cookie,您将需要在循环中使用不同的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    相关资源
    最近更新 更多