我正在尝试通过我的java程序构造一个URL,该程序会将URL发送到我的php服务器,因此在我的mysql数据库中插入一行。我能够手动将 URL(通过我的浏览器地址栏)发送到我的 php 服务器,并成功地在我的表中插入一行。
但是,当我在我的 java 程序中构造 URL 并尝试将请求发送到 php 服务器时,我没有插入任何行。奇怪的是,当我手动将其放入地址栏中时,java程序构造的url可以正常工作。我的php错误日志中确实出现了错误,但据我所知,一切看起来都很好。
任何事情都有帮助,谢谢。
爪哇岛:
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 注入。