我试图从我的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东西...
提前致谢!
〜瑞恩