Question

I have several PHP sripts saved in my /var/www/ directory. I can run them from my browser but Java can't see them.

Here is output from browser:

enter image description here

And from Java:

DB: Error executing script: get_profile_ids
HTTP/1.1 404 Not Found [Date: Tue, 16 Apr 2013 11:52:40 GMT, Server: Apache/2.2.22 (Ubuntu), Vary: Accept-Encoding, Content-Length: 288, Keep-Alive: timeout=5, max=100, Connection: Keep-Alive, Content-Type: text/html; charset=iso-8859-1]
DB: Result: 

My Java code:

public class ServerTest {

    public static void main(String [] args) {

        callPHPScript("get_print_profile_ids", new ArrayList<NameValuePair>());

    }

    public static String callPHPScript(String scriptName, List<NameValuePair> parameters) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost/" + scriptName);
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            post.setEntity(new UrlEncodedFormEntity(parameters));

            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() != 200)
            {
                System.out.println("DB: Error executing script: " + scriptName);
                System.out.println(response.toString());
            }
            else {
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
                line = "";
                while ((line = rd.readLine()) != null) {
                    stringBuilder.append(line);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("DB: Result: " + stringBuilder.toString());
        return stringBuilder.toString();
    }
}

And my PHP Script:

<?php
include('connect_db.php');
include('tools.php');

$query = 'SELECT id FROM fingerprint_profiles';

$result = mysql_query($query);
echo($result);

?>

Does anybody know why this might happen??

Was it helpful?

Solution

You are missing the .php, change:
HttpPost post = new HttpPost("http://localhost/" + scriptName);
to
HttpPost post = new HttpPost("http://localhost/" + scriptName + ".php");

OTHER TIPS

here's the hint:

System.out.println("DB: Error executing script: " + scriptName);

is logging

DB: Error executing script: get_profile_ids

change your test call to

callPHPScript("get_profile_ids.php", new ArrayList<NameValuePair>());

and you'll likely get a successful result.

Your Java code calls get_profile_ids while your browser screenshot shows get_profile_ids.php. Change your Java code to call get_profile_ids.php as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top