循环使用CFHTTP cookie并设置cookie

时间:2014-12-08 20:52:48

标签: coldfusion

我正在尝试遍历来自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对象。循环错误。

1 个答案:

答案 0 :(得分:0)

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,你将需要在循环中使用不同的代码。