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

时间:2013-04-27 03:28:04

标签: java php

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

基本上我编写了这个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服务器上执行此操作时,它不会:/

感谢您的帮助:)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

嗯,来自URLConnection的javadoc:

  

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

     

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

     

3)使用,实现与远程对象的实际连接   连接方法。

     

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

您似乎没有调用URLConnection#connect