Https Sockets in CFC
A while back I needed to interact with raw https sockets from Cold Fusion, and the https setting for CFX_RawSocket stopped working. (I think I configuration change on the target machine made this process fail.) Unable to deal with trying to get support for a free product, I felt compelled to write my own way of doing it. Some one in my office had writted a CFX tag very similar to CFX_RawSocket, so I had a starting point.
Turns out, it can be done completely without resorting to CFX writing. It can be done completely using Cold Fusions built in ability to handle Java Objects. I figured I would share the source if anyone else needed it.
<!--- Use the sslfactory to create an object based on the Socket class --->
<cfset factory = CreateObject ("java","javax.net.ssl.SSLSocketFactory").getDefault()>
<cfset sock = factory.createSocket(arguments.host,arguments.port)>
<cfset sock.startHandshake()>
<!--- Connect to output stream --->
<cfset sout = sock.getOutputStream()>
<!--- Connect to input stream --->
<cfset out = createObject( "java", "java.io.PrintWriter" ).init(sout)>
<cfset sinput = sock.getInputStream()>
<!--- I have no clue what is going here. I just know it has to be done this way. --->
<cfset inputStreamReader= createObject( "java", "java.io.InputStreamReader").init(sinput)>
<cfset input = createObject( "java", "java.io.BufferedReader").init(InputStreamReader)>
<!--- Send message to server. --->
<cfset out.println("#arguments.message#")>
<cfset out.println()>
<cfset out.flush()>
<!--- Get back response. --->
<!--- I only need the response header, so I only get the first line --->
<cfset results=input.readLine()>
<cfset sock.close()>
<cfreturn results>
</cffunction>
I've gotten that sometimes when https is not active on the webserver I'm contacting. Alternatively, it doesn't like the ssl cert the webserver is using. 


Any ideas?
Posted by: Jason at June 7, 2006 1:03 PM