0

我在这里和整个互联网上查看了很多其他线程,我似乎无法解决这个问题..

所以基本上我已经写了这个Java代码:

public void sendReport(CommandSender sender, Player target, String reason)
{
    HttpURLConnection connectionStandard = null;
    String email = config.getString("rp.site.email");
    String password = config.getString("rp.site.password");
    String senderName = sender.getName();
    String targetName = target.getDisplayName();
    String reasonString = reason;

    try
    {

        URL url = new URL("http://www.website.net/folder/connect.php");    
        HttpURLConnection request = (HttpURLConnection)url.openConnection();
        request.setRequestProperty("Content-type","application/x-www-form-urlencoded");
        request.setRequestMethod("POST");
        request.setDoOutput(true);
        OutputStreamWriter post = new OutputStreamWriter(request.getOutputStream());
        String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email.toString(), "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password.toString(), "UTF-8");
        data += "&" + URLEncoder.encode("reporter", "UTF-8") + "=" + URLEncoder.encode(senderName, "UTF-8");
        data += "&" + URLEncoder.encode("reported", "UTF-8") + "=" + URLEncoder.encode(targetName, "UTF-8");
        data += "&" + URLEncoder.encode("reason", "UTF-8") + "=" + URLEncoder.encode(reasonString, "UTF-8");
        post.write(data);
        post.flush();
    } catch (MalformedURLException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    } finally
    {
        if(null != connectionStandard)
        {
            connectionStandard.disconnect();
        }
    }
}


基本上,我的 php 代码看起来像这样:

<?php

 require_once "db.php";

 $reporter->startSecureSession();

 foreach ($_POST as $post => $value) {
  $post = strip_tags($post);
  $value = strip_tags($value);
 }

 if(!(isset($_POST['email'])) or (!isset($_POST['password'])) or (!isset($_POST['reporter'])) or (!isset($_POST['reported'])) or (!isset($_POST['reason']))) {
  exit("Invalid Request");
 }

 $password = hash("sha512", $_POST['password']);

 $reporter->login(array("email" => $_POST['email'], "password" => $password), "plugin");
 if($reporter->validateClient()) {

  $reporter->sendReport($_POST);
  header("Location: logout.php");
  exit();

 } else {
  exit();
 }

?>

当我通过 chrome 将我的详细信息发送到网页时,它可以工作并将内容发送到我的数据库,但是当我通过发送请求的命令在我的 bukkit 服务器上执行此操作时,它不会:/

谢谢您的帮助 :)

4

2 回答 2

3

您可以使用 Apache httpclient 4.x 从 Java 发送 GET/POST 请求。

String url = "http://www.website.net/folder/connect.php";
HttpPost method = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
formparams.add(new BasicNameValuePair("email", email.toString()));
formparams.add(new BasicNameValuePair("password", password.toString()));
formparams.add(new BasicNameValuePair("reporter", senderName));
formparams.add(new BasicNameValuePair("reported", targetName));
formparams.add(new BasicNameValuePair("reason", reasonString));
UrlEncodedFormEntity entity = null;
try {
    entity = new UrlEncodedFormEntity(formparams, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
method.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(method);
于 2013-04-27T04:50:37.563 回答
0

好吧,来自 URLConnection 的 javadoc:

1) 通过在 URL 上调用 openConnection 方法来创建连接对象。

2)设置参数和一般请求属性被操纵。

3) 使用 connect 方法建立到远程对象的实际连接。

4) 远程对象变为可用。可以访问远程对象的标头字段和内容。

你似乎没有打电话URLConnection#connect

于 2013-04-27T03:38:38.370 回答