I am using a ColdFusion component to publish a web service and I want that web
service to set a cookie. However, this does not appear to always work.
Specifically, if I invoke the web service from the browser by entering a URL
such as "service.cfc?method=name" in the address bar, then the cookie is set
properly. If I use <cfinvoke> the cookie is not set. If I use the EasySoap C++
library then the cookie is set if I use <cfheader> to set it, but is not set if
I use <cfcookie>.
Please see the attached code samples. "cookies.cfc" is the component used to
publish the web service, "cookies.cfm" is a simple test programme which invokes
the web service either using <cfinvoke> or with the web browser (the second
time you invoke the web service you should see that the cookies were set the
last time you invoked it, but this only happens if you invoke it using the
browser. A simple C++ programme to invoke the web service (using EasySoap,
which in turn uses WinInet) returns "Cookie.EchoLast_2=99" meaning that only
cookies set using <cfheader> are actually being set.
Please explain what on earth it is doing and what is the correct/approved way
of setting cookies in a web service.
Thanks!
cookies.cfc
~~~~~~~~~
<cfcomponent displayname="Cookie Tester">
<cffunction name="EchoLast" access="remote" returntype="string" output="no">
<cfargument name="TestString" type="string" required="yes">
<cfif IsDefined("Cookie.EchoLast_1")>
<cfset retval = "Cookie.EchoLast_1=#Cookie.EchoLast_1#">
<cfelse>
<cfset retval = "">
</cfif>
<cfif IsDefined("Cookie.EchoLast_2")>
<cfif retval NEQ "">
<cfset retval = "#retval# , ">
</cfif>
<cfset retval = "#retval#Cookie.EchoLast_2=#Cookie.EchoLast_2#">
</cfif>
<cfif retval EQ "">
<cfset retval = "[NOT SET]">
</cfif>
<cfcookie name="EchoLast_1" value="#Arguments.TestString#">
<cfheader name="Set-Cookie" value="EchoLast_2=#Arguments.TestString#;
path=/">
<cfreturn retval>
</cffunction>
</cfcomponent>
cookies.cfm
~~~~~~~~~
<cfif IsDefined("Form.TestString")>
<cfset webservice =
"http://#CGI.SERVER_NAME##Replace(CGI.SCRIPT_NAME,'/cookies.cfm','/cookies.cfc')
#?wsdl">
<cfinvoke method="EchoLast" returnvariable="CompletionMessage"
webservice="#webservice#">
<cfinvokeargument name="TestString" value="#Form.TestString#">
</cfinvoke>
<cfif IsNumeric(Form.TestString)>
<cfset Form.TestString = (Form.TestString + 1)>
</cfif>
<cfelse>
<cfset Form.TestString = 99>
</cfif>
<html>
<head>
<title>Cookie Tester</title>
</head>
<body>
<form action="cookies.cfm" method="post">
<table border=0 cellspacing=1 cellpadding=5>
<cfif IsDefined("Variables.CompletionMessage")>
<tr><td colspan=2 align="center"><table border="1" cellpadding="15"
align="center"><tr><td><b>RETURN VALUE: </b></td><td
align="center"><cfoutput>#HTMLEditFormat(CompletionMessage)#</cfoutput></td></tr
></table></td></tr>
<tr><td colspan=2> </td></tr>
</cfif>
<tr><th>String To Send:</th><td><cfoutput><input type="Text"
name="TestString" value="#HTMLEditFormat(Form.TestString)#" size="18"
maxlength="75"></cfoutput></td></tr>
<tr bgcolor="lightblue"><td align="left"><cfoutput><a
href="cookies.cfc?method=EchoLast&TestString=#HTMLEditFormat(Form.TestString)#"
target="_blank">Call-Via-URL</a></cfoutput></td><td align="right"><input
type="submit" name="x" value=" GO "></td></tr>
</table>
</form>
</body>
</html>
GArlington - 18 Sep 2007 11:55 GMT
> I am using a ColdFusion component to publish a web service and I want that web
> service to set a cookie. However, this does not appear to always work.
[quoted text clipped - 90 lines]
> </body>
> </html>
Setting cookies works on the computer which requests the service,
<cfinvoke ...> requests your webservice so you can expect the cookies
to be set on your CF server computer...