Java找不到PHP脚本但可以在浏览器中使用

时间:2013-04-16 11:58:57

标签: java php mysqli ubuntu-12.10

我的/ var / www /目录中保存了几个PHP sripts。我可以从我的浏览器运行它们,但Java无法看到它们。

以下是浏览器的输出:

enter image description here

来自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: 

我的Java代码:

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();
    }
}

我的PHP脚本:

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

$query = 'SELECT id FROM fingerprint_profiles';

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

?>

有人知道为什么会这样吗?

3 个答案:

答案 0 :(得分:5)

你错过了.php,改变:
HttpPost post = new HttpPost("http://localhost/" + scriptName);

HttpPost post = new HttpPost("http://localhost/" + scriptName + ".php");

答案 1 :(得分:1)

这是提示:

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

正在记录

DB: Error executing script: get_profile_ids

将测试呼叫更改为

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

你可能会得到一个成功的结果。

答案 2 :(得分:1)

当您的浏览器屏幕截图显示get_profile_ids时,您的Java代码会调用get_profile_ids.php。更改Java代码也可以调用get_profile_ids.php

相关问题