Https Sockets in CFC
<!--- ############# --->
<!--- 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