我正在尝试从我的 Bukkit Minecraft 插件向我的网页发出 HTTP Post 请求...
现在这是我用于 java 端的代码:
try {
URL url = new URL("http://[::1]/reporter/getreport.php");
String username = this.getConfig().getString("username");
String password = this.getConfig().getString("password");
String query = "username=" + URLEncoder.encode(username, "UTF-8");
query += "&";
query += "password=" + URLEncoder.encode(password, "UTF-8");
query += "&";
query += "sender=" + URLEncoder.encode(sender.getName(), "UTF-8");
query += "&";
query += "target=" + URLEncoder.encode(target.getName(), "UTF-8");
query += "&";
query += "reason=" + URLEncoder.encode(reason, "UTF-8");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(query.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(query);
wr.flush();
wr.close();
connection.disconnect();
sender.sendMessage("Reported!");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
现在我尝试使用 XAMPP 来执行此操作,其中存储了我的本地数据库和页面,它使用 localhost 连接到。
这是我用来连接的 PHP 代码:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once __DIR__ . '/inc/db.php';
$username = $_POST['username'];
$password = $_POST['password'];
$sender = $_POST['sender'];
$target = $_POST['target'];
$reason = $_POST['reason'];
try {
$SQL = $dbh->prepare('INSERT INTO reports (username, password, sender, target, reason, time) VALUES (:username, :password, :sender, :target, :reason, :time)');
$SQL->bindParam(':username', $username);
$SQL->bindParam(':password', $password);
$SQL->bindParam(':sender', $sender);
$SQL->bindParam(':target', $target);
$SQL->bindParam(':reason', $reason);
$SQL->bindParam(':time', date('Y-m-d H:i:s'))
$SQL->execute();
} catch(PDOException $e) {
}
?>
谁能发现问题?这几天一直困扰着我,我在任何地方都找不到解决方案......
编辑:: 我不想使用 Apache 的 HttpClient 的东西......
提前致谢!
〜瑞恩