gpt4 book ai didi

java - HttpURLConnection 到 php 但没有 mysql post?

转载 作者:可可西里 更新时间:2023-11-01 17:26:19 25 4
gpt4 key购买 nike

我正在尝试通过我的 java 程序构建一个 URL,它将 URL 发送到我的 php 服务器,因此将一行插入到我的 mysql 数据库中。我能够手动将 URL(通过我的浏览器地址栏)发送到我的 php 服务器并成功地在我的表中插入一行。

但是,当我在我的 java 程序中构建 URL 并尝试将请求发送到 php 服务器时,我没有插入任何行。奇怪的是,当我手动将 java 程序构造的 url 放入地址栏时,它会起作用。我的 PHP 错误日志中确实出现了错误,但据我所知,一切看起来都很好。

任何事情都有帮助,谢谢。

Java:

    public void updateDB() throws IOException {


final String USER_AGENT = "Mozilla/5.0";
final String POST_URL = "https://example.website/php/php.php?";
final String POST_PARAMS = "charName=" + "\"myname\"" + "&logType=" + "\"Magic_logs\"" + "&magicPrice=" + pricePerMagicLog + "&yewPrice=" + pricePerYewLog + "&totalWealth="+ totalWealth;

URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);


con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();


int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);

if (responseCode == HttpsURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}

println(POST_URL+POST_PARAMS);

PHP:

<?php require_once('muleconnect.php');
$charName = $_GET['charName'];
$logType = $_GET['logType'];
$magicPrice = $_GET['magicPrice'];
$yewPrice = $_GET['yewPrice'];
$totalWealth = $_GET['totalWealth'];

$sql = "INSERT INTO table(char_name, log_type, magic_log_price,
yew_log_price, total_wealth)
VALUES ($charName, $logType, $magicPrice, $yewPrice, $totalWealth)";


$query = $muleconnection->query($sql);
echo 'OK..';

?>

php错误日志:

[08-Oct-2017 14:00:47 America/New_York] PHP Notice:  Undefined index: charName in /home/dumamwny/public_html/php/php.php on line 4
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: logType in /home/dumamwny/public_html/php/php.php on line 5
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: magicPrice in /home/dumamwny/public_html/php/php.php on line 6
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: yewPrice in /home/dumamwny/public_html/php/php.php on line 7
[08-Oct-2017 14:00:47 America/New_York] PHP Notice: Undefined index: totalWealth in /home/dumamwny/public_html/php/php.php on line 8

成功构造 url 字符串(手动工作,而不是编程)

https://example.website/php/php.php?charName=%22example_name%22&logType=%22Magic%20logs%22&magicPrice=1174&yewPrice=340&totalWealth=7145753

最佳答案

您正在使用 POST 构建请求.

URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");

但是用 GET 检索它

$charName = $_GET['charName'];
$logType = $_GET['logType'];
$magicPrice = $_GET['magicPrice'];
$yewPrice = $_GET['yewPrice'];
$totalWealth = $_GET['totalWealth'];

还有这里

$sql = "INSERT INTO table(char_name, log_type, magic_log_price, 
yew_log_price, total_wealth)

我想,table应该是实际的 <<table_name>> .另外,考虑使用准备好的语句来避免 SQL 注入(inject)。

关于java - HttpURLConnection 到 php 但没有 mysql post?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634528/

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