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.
<!— https_socket —>
<!— ############# —>
<cffunction access=”package” name=”https_socket” output=”false” returntype=”string” hint=”Allows socket communication over https to occur. “>
<cfargument name=”host” type=”string” required=”yes” hint=”The host to send message to. “>
<cfargument name=”message” type=”string” required=”yes” hint=”The message to send to the host.”>
<cfargument name=”port” type=”numeric” required=”no” default=”443″ hint=”The port to connect to. ” >
<!— 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>
Thanks for this. I am trying to use it but it takes forever then I get a ” Connection reset” error.
Any ideas?
LikeLike
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.
Have you tried connecting to multiple webservers?
LikeLike