Skip to main content
Mar 31, 2010 7:44 PM UTC
Answered

How to communicate with a remote application that uses http post to request connection

Ok, I will try to make this simple as to avoid any

confusion.

I am trying to receive connection requests from remote applications installed on user pc's.

The application uses POST method to send the request to connect to my database, and if required update some tables.

The application is not a webpage. I have no access to the internal workings of the application nor do I have any specific information about how it sends its POST. I am sure of the following:

The app will POST to my .cfm page

"IsConnectionAllowed" = "yes"

This is asking my cfm page if its ok to connect. I would need to use a <cfif> to verify the the first part of the POST is in fact

"IsConnectionAllowed" = "yes"

If this returns true, (the app also uses to POST to pass "username" and "password") my .cfm page will then perform a query and check the username and password against the database. If a matching record is found, the application expects my page to reply with

"#Answer# OK- connected;"

Once the answer is given the application will then respond with data to insert or update my tables.

Now, I know how to do the queries and update tables etc, but I have never received data from an application before.

I tried using the <cfform> tag with a mehod of GET, and then trying to send my reply with <cfoutput>, but that didn't work. The application doesn't give any feedback or error messages. It either connects or not.

I also tried the <cfhttp> tag, but that didnt work either. I am not very experinced with coldfusion. I used it years ago and am just getting back into it.

I am sure this is a pretty easy thing to do, but my methods arent working.

Using the above information, can somone please show me how I would accomplish this communication? I can then use the same method to retrieve the data for my table updates and queries.

Here is a sample of what I have already tried:

<cfform method="get">

<cfif>

"IsConnectionAllowed" = "yes"

<!-- I need to query a username or password. But for testing I will just give the OK to connect -->

<cfoutput>#Answer# Ok - connected;</cfoutput>

<cfelse>

<!-- When I query username and password, if the query returns no records I deny the connection -->

<cfoutput>#Answer# Not connected;</cfoutput>

</cfif>

</cfform>

BTW, I have examples of this being done using .php and .asp. I need to do this using .cfm. I can post the either the .php or.asp code if it will be helpful in converting the function into a .cfm page.

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Fernis
    FernisCorrect answer
    Inspiring
    Mar 31, 2010 10:50 PM UTC

    Just as I speculated, you're just returning plain text - i.e. just like generating a normal web page.

    The code below loosely mimics your PHP code. I didn't test it, so it might contain errors. I used tags instead of cfscript, since you might be working with ColdFusion 8 and not 9. I disable all coldfusion output and wrap all that is to be output, between <cfoutput></cfoutput>. This way no extra line breaks or spaces get ever sent. <cfscript>ed syntax would of course take care of this, which I more happily use when working with CF9.

    <cfsetting enablecfoutputonly="yes">
    <cfif structKeyExists(Form,"isConnectionAllowed") AND form.isConnectionAllowed EQ "yes">
       
        <cfif databaseConnexion EQ FALSE>
            <cfoutput>##Answer## Error - unable to connect to mySQL database;</cfoutput>
            <cfabort>
        </cfif>

        <cfquery name="qResult" datasource="myDatasourceNameHere">
        SELECT * FROM user_profile
        WHERE UserName = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.UserName#">
        AND Password = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.Password#">
        </cfquery>
       
        <cfif qResult.recordcount eq 0>
            <cfoutput>##Answer## Error - Username does not exist or wrong password;</cfoutput>
            <cfabort>
        </cfif>

        <cfoutput>##Answer## OK - connected;</cfoutput>
    </cfif>


    What still puzzles me is; does the client app make separate connections to your server or not - to first check if connection can be made, and then for username/password? Obviously not, although you first mentioned it might. In your code example, username and password can only be checked if isConnectionAllowed is posted on the same request.

    I'd imagine the client app posts these three fields (isConnectionAllowed, username, password) also whenever it makes subsequent calls to update query data, because there's no any session handling implemented in the PHP code.

    Hope this helps. *shrug*

    --

    - Fernis - fernis.net - ColdFusion Developer For Hire

    7 replies

    Fernis
    Inspiring
    Mar 31, 2010 9:55 PM UTC

    To me it sounds you just need to output data as someone posts a form to your template.

    You don't need <cfhttp> or <cfform> or anything to just write out a response code. Just plain CF Output should be enough.

    It's another matter then, how the client application keeps a session for example, if the username/password authentication is ok - how do you know the next time that the application that is posting, is the same one which authenticated OK previously...

    Since we might be missing a ton of details here, posting the php code would at least help a great deal.

    -Fernis

    Translate
    Mar 31, 2010 10:25 PM UTC

    Hi Fernis

    ,

    Thank you for your reply. This data being posted is not a form. The data is being sent however using http post. Also, the data that will be sent following my page's acceptance to the conenction will all be done in the same session. Once the data is sent, the remote application terminates the connection. If in the future more data needs to be updated, it will then send a new request using the same POST method to the page. I hope that makes sense. Maybe the php code will be a little more clear as to what I am trying to achieve

    Here the code php code I am trying to translate to cfm:

    if($_POST['IsConnectionAllowed']=="yes")
    {
    // the value "$databaseconnexion" is set to true in common.php if the SQL connexion is ok
    if($databaseconnexion==FALSE)
    {
      echo "#Answer# Error - unable to connect to mySQL database;";
      return;
    }

    // check username and password
    if($query = "SELECT * FROM user_profile WHERE UserName='$_POST[UserName]' and Password='$_POST[Password]'")
        $result=@mysql_query($query);if(!$result){echo "#Answer# SQL Error - ".mysql_error();return;}
    if(mysql_num_rows($result)==0)
    {
      echo "#Answer# Error - Username don't exist or wrong password;";
      return;
    }

    //If we are here, username, password and connexion to SQL database is ok so we reply it's ok.
    // this will also send the welcome message.
    echo "#Answer# Ok - connected;";
    printf("#welcome#%s#endwelcome#",$cfg['welcome_message']);
    WriteDebug();
    exit();
    }

    After the above there are update queries run...I will be able to deal with those provided I can get a grip on how to handle the initial connection request that comes via the POST.

    Translate
    Fernis
    FernisCorrect answer
    Inspiring
    Mar 31, 2010 10:50 PM UTC

    Just as I speculated, you're just returning plain text - i.e. just like generating a normal web page.

    The code below loosely mimics your PHP code. I didn't test it, so it might contain errors. I used tags instead of cfscript, since you might be working with ColdFusion 8 and not 9. I disable all coldfusion output and wrap all that is to be output, between <cfoutput></cfoutput>. This way no extra line breaks or spaces get ever sent. <cfscript>ed syntax would of course take care of this, which I more happily use when working with CF9.

    <cfsetting enablecfoutputonly="yes">
    <cfif structKeyExists(Form,"isConnectionAllowed") AND form.isConnectionAllowed EQ "yes">
       
        <cfif databaseConnexion EQ FALSE>
            <cfoutput>##Answer## Error - unable to connect to mySQL database;</cfoutput>
            <cfabort>
        </cfif>

        <cfquery name="qResult" datasource="myDatasourceNameHere">
        SELECT * FROM user_profile
        WHERE UserName = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.UserName#">
        AND Password = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.Password#">
        </cfquery>
       
        <cfif qResult.recordcount eq 0>
            <cfoutput>##Answer## Error - Username does not exist or wrong password;</cfoutput>
            <cfabort>
        </cfif>

        <cfoutput>##Answer## OK - connected;</cfoutput>
    </cfif>


    What still puzzles me is; does the client app make separate connections to your server or not - to first check if connection can be made, and then for username/password? Obviously not, although you first mentioned it might. In your code example, username and password can only be checked if isConnectionAllowed is posted on the same request.

    I'd imagine the client app posts these three fields (isConnectionAllowed, username, password) also whenever it makes subsequent calls to update query data, because there's no any session handling implemented in the PHP code.

    Hope this helps. *shrug*

    --

    - Fernis - fernis.net - ColdFusion Developer For Hire

    Translate