TerrenceRyan.com

I'm a 35 year old redhead geek from Philly.
I'm currently a Developer Evangelist for Adobe.
Also the author of Driving Technical Change

Https Sockets in CFC

2 Comments

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>

2 responses so far ↓

  • 1 Jason

    Thanks for this. I am trying to use it but it takes forever then I get a " Connection reset" error.

    Any ideas?

  • 2 Terrence Ryan

    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?

Leave a Comment









Categories

Monthly Archives

Tag Cloud

coldfusion web development flex coldfusion builder appearances squidhead coldfusion builder extensions higher ed html5 flash builder air mobile android driving technical change adobe apptacular running a coldfusion shop adobemax06 movable type flash catalyst flash css blackberry adobemax07 adobemax08 hero finicky adobemax09 holy crap i’m a mobile developer centaur phonegap basecamp cfc whichelement unfuddle motorola metablog irrational characters ios git evangelism edge devices code reviews ant wharton subversion security rhino philly philadelphia multidevice knowledge@wharton jobs javascript java browserlab adobemax10 adobe tv unfuddlecfc svnauth.cfc semantic html semantic html responsive web design qnx nlb linux jquery mobile it github flexorg fireworks eclipse dreamweaver apps apple adobemax11