2012-08-06 62 views
2

我正在努力弄清楚如何閱讀cfhttp.responseHeader的內容。我試圖訪問一個網站,在響應中發送一些cookie。我需要從響應中提取它們。然後發送所有未來請求的cookie值。我試圖用下面的代碼:Coldfusion CFHTTP.Responseheader

<cfloop collection = #cfhttp.responseHeader# item = "httpHeader"> 
    <cfset value = cfhttp.responseHeader[httpHeader]> 
    <cfif IsSimpleValue(value)> 
     <cfoutput> 
     #httpHeader# : #value#<BR> 
     </cfoutput> 
<cfelse> 
     <cfloop index = "counter" from = 1 to = #ArrayLen(value)#> 
     <cfoutput> 
     #httpHeader# : #value[counter]#<BR> 
     </cfoutput> 
</cfloop> 
</cfif> 

但是,這引發以下錯誤

Object of type class coldfusion.util.FastHashtable cannot be used as an array 


The error occurred in C:/inetpub/wwwroot/cfdocs/Response.cfm: line 22 

20 :  </cfoutput> 
21 : <cfelse> 
22 :  <cfloop index = "counter" from = 1 to = #ArrayLen(value)#> 
23 :  <cfoutput> 
24 :   #httpHeader# : #value[counter]#<BR> 

回答

3

您可以檢索餅乾這樣的:

<cfset cookies = cfhttp.responseHeader["set-cookie"] /> 

<cfdump var="#cookies#" /> 

然後可以使用該cookie結構數據,使您的後續請求。

0

問題是,您正在嘗試循環結構,但將其視爲數組。您需要使用「集合」來遍歷結構。

<cfloop collection="#cfhttp.responseHeader['set-cookie']#" item="sKey"> 
    ..... 
</cfloop> 
0

這是我在Ben Nadel網站上使用引用獲取頭文件cookies的腳本。

public struct function GetResponseCookies(required struct Response){ 
    var LOCAL = {}; 
    LOCAL.Cookies = {}; 

    if(!StructKeyExists(ARGUMENTS.Response.ResponseHeader,"Set-Cookie")){ 
     return LOCAL.Cookies; 
    } 

    LOCAL.ReturnedCookies = ARGUMENTS.Response.ResponseHeader[ "Set-Cookie" ]; 

    if(!isStruct(LOCAL.ReturnedCookies)){ 
     return LOCAL.Cookies; 
    } 

    for(LOCAL.CookieIndex in LOCAL.ReturnedCookies){ 
     LOCAL.CookieString = LOCAL.ReturnedCookies[ LOCAL.CookieIndex ]; 

     for(LOCAL.Index =1; Local.Index != ListLen(LOCAL.CookieString, ';'); LOCAL.Index++){ 
      LOCAL.Pair = ListGetAt(LOCAL.CookieString,LOCAL.Index,";"); 
      LOCAL.Name = ListFirst(LOCAL.Pair, "="); 

      if(ListLen(LOCAL.Pair, "=") > 1){ 
       LOCAL.Value = ListRest(LOCAL.Pair, "="); 
      } else { 
       LOCAL.Value = ""; 
      } 

      if(LOCAL.Index EQ 1){ 
       LOCAL.Cookies[ LOCAL.Name ] = {}; 
       LOCAL.Cookie = LOCAL.Cookies[ LOCAL.Name ]; 
       LOCAL.Cookie.Value = LOCAL.Value; 
       LOCAL.Cookie.Attributes = {}; 
      } else { 
       LOCAL.Cookie.Attributes[ LOCAL.Name ] = LOCAL.Value; 
      } 
     } 
    } 
    return LOCAL.Cookies; 
}