JAVA中的HTML POST不起作用,没有给出错误

时间:2016-09-21 13:12:32

标签: java php html mysql xampp

我一直试图调试我的代码一段时间,并试图了解什么是错的,但似乎没有任何帮助,所以我想也许有人在这里可以帮助我。

这是我的Java代码!

public class sql {

    static String login_url = "http://localhost/index.php";

    public static String main(String x, String y){

        try{

            String username = x;
            String password = y;

            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

            String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
                    +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            // LÄSER FRÅN HEMSIDAN

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result="";
            String line="";
            while((line = bufferedReader.readLine()) != null){

                result += line;
            }



            //AVSLUTA CONNECTION
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            return result;

        }catch(Exception e){
            e.printStackTrace();
        }

        return null;

    }

    public static void main(String[] args){

        main("usr","password");


    }
}

和我的PHP代码:

<?php
$db_name = "smoothie";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";

$usr = $_POST["username"];
$pass = $_POST["password"];

$conn = mysql_connect($server_name,$mysql_username,$mysql_password,$db_name);

$query = "INSERT INTO members (username, password)
VALUES ('$usr','$pass')";


?>

我目前正在运行php文件和mysql通过xampp!

真正困扰我的是,在日食和网站中都没有显示任何错误。有人可以看看代码,看看有什么缺失?

0 个答案:

没有答案