Helpful Information
 
 
Category: ColdFusion
Coldfussion HTTP sockets

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>

Found Solution after I posted this based on a tutoral I found on internet

<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>










privacy (GDPR)