为什么我的HTTP POST请求发送到我的网页


Why wont my HTTP POST request send to my webpage?

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

基本上我写了这个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服务器通过命令发送请求,它不:/

谢谢你的帮助

您可以使用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);

从URLConnection的javadoc:

1)通过调用openConnection方法创建连接对象

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

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

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

您似乎没有调用URLConnection#connect