Friday, January 25, 2013

cfhttp in java

We had some problems with CF8 not recognizing SANs SSL certificates. The certificates were valid and properly configured, but cfhttp constantly delivered an "I/O Exception:".

The only way I could find around this was to make the calls in Java. In case it helps someone, the Java I used to make a post to a soap web service was basically as follows:

<cfsavecontent variable="requestXML"><?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>

  </soap:Header>
  <soap:Body>

  </soap:Body>
</soap:Envelope>
</cfsavecontent>


<cfscript>
        myUrl = "xxx"; // web service url
        objUrl = createobject("java","java.net.URL").init(myUrl);
        conn = objUrl.openConnection();

        //configure the request
        conn.setDoOutput(true);
        conn.setUseCaches(false);
         conn.setRequestMethod("POST");
        conn.setRequestProperty("SOAPAction", "xxx my soap action"); 
       
        //output stream actions
        ostream = conn.getOutputStream();
        ostream.write(Javacast("String",requestxml).toString().getBytes());
        ostream.flush();
        ostream.Close();
        // set input
        inS =createobject("java","java.io.InputStreamReader").init(conn.getInputStream());
        inVar = createObject("java","java.io.BufferedReader").init(inS);

        builder = createObject("java","java.lang.StringBuilder").init(javacast("int",1000));
        line    = "";



        do
        {
           line = inVar.readLine();
           lineCheck = isDefined("line");
           if(lineCheck)
           {
              builder.append(line);
           }
        } while(lineCheck);


        retvar = builder.toString();

</cfscript>
 <cfoutput>#retvar#</cfoutput>



The problems seems to be fixed in newer versions of CF.  In CF 10 I was able to make the requests to the servers with SANs certificates without a problem.

2 comments:

  1. Really its a amazing article that I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. Thank you for sharing such a great blog with us. Expecting for your update.
    Dotnet Training in Chennai

    ReplyDelete