HttpURLConnection到php但没有mysql帖子?

时间:2017-10-08 18:22:15

标签: java php mysql http

我试图通过我的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

1 个答案:

答案 0 :(得分:1)

您正在使用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注入。