Hello I am very new to ColdFusion and trying to create a socket so that I can do remote HTTP connection. I basically want to make a general function that will request data from a server and then return data back. There probably a built in function to do this work but I been unable to find it. I want it to be really flexible and work with a range of different client TCP/IP connections setups and do POST,GET,HEAD Requests. I not interested in doing multithreading and want it to wait for the data to come back after a request is made. I found a solution for SSH connections but not one for simple http connection. This code just stops ColdFusion in it track and is unable to process it further presummly something to do with the way ColdFusion handles data types. Also how do I pass default parmas when using the cfscript tag Any help will be greatly appreciated Thanks Patrick <!--- *********************************************************** ---> <!--- https_socket ---> <!--- Allows socket communication over https to occur. ---> <!--- *********************************************************** ---> <cfscript> function useSocket() { // Go into JAVA and grab the relevent classes socket=CreateObject("java","java.net.Socket"); inputStream=CreateObject("java","java.io.DataInputStream"); outputStream=CreateObject("java","java.io.DataOutputStream"); // Call Constructer of socket socket.init("192.168.0.1",80); // LINK INPUT / OUTPUT STREAMS TO SOCKETS by create instances of these inputStream.init(socket.getInputStream()); outputStream.init(socket.getOutputStream()); outputStream.writeBytes("GET / HTTP/1.0\n"); outputStream.writeBytes("host: 192.168.0.1\n"); outputStream.writeBytes("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729) FirePHP/0.1.2\n"); outputStream.writeBytes("\n\n"); return inputStream.readLine(); } </cfscript> --- <cfinclude template = "comp/https_sockets.cfc"> <cfscript> test = useSocket(); </cfscript> <cfoutput>#test#</cfoutput>
Is there a reason you cannot use cfhttp? http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_g-h_09.html#3989287
Thanks For your Help, Sorry I didn't realice their was a specific tag. I did find a solution using java sockets based upon a tutoral I found. <cfscript> function useSocket(host,port) { CurrentLine = ""; Data = ""; // Go into JAVA and grab the relevent classes clientSocketConnection=CreateObject("java","java.net.Socket"); clientSocketConnection.init(host,port); // Into Network Card inputStreamReader=CreateObject("java","java.io.InputStreamReader"); inputStreamReader.init(clientSocketConnection.getInputStream()); BufferedReader=CreateObject("java","java.io.BufferedReader"); BufferedReader.init(inputStreamReader); // Out To Network Card outputStream = clientSocketConnection.getOutputStream(); PrintWriter=CreateObject("java","java.io.PrintWriter"); PrintWriter.init(outputStream,true); // Write Output Msg PrintWriter.println("GET / HTTP/1.0"); PrintWriter.println("host: "&host); PrintWriter.println("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729) FirePHP/0.1.2"); PrintWriter.println(); PrintWriter.println(); // Gather Message From Network Card // Cold Fussion cannot use NULL so we have to use isDefined which checks varable for null CurrentLine=BufferedReader.readLine(); while(IsDefined("CurrentLine")) { Data &= CurrentLine&chr(10); CurrentLine=BufferedReader.readLine(); } return Data; } </cfscript>
Yes, but cfhttp can do all that in about 1/4 the code ;-) The java stuff is cool, but is often unnecessary. Honestly, if you are new to CF, I would learn the core language first. There are some great CF tags and functions which are usually a lot simpler to use. After you have the basics down, then start exploring the java possibilities. (Unless you are a java person already).
Also, you might investigate coldfusion bindings. Depending on what you are doing, it might save you even more code.