gpt4 book ai didi

java - 为什么我的 HTTP POST 请求无法发送到我的网页?

转载 作者:行者123 更新时间:2023-12-01 23:51:31 51 4
gpt4 key购买 nike

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

基本上我已经编写了这段 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);

关于java - 为什么我的 HTTP POST 请求无法发送到我的网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16248022/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com