0
我在JAVA中有一个小程序,我想创建一个登录系统,但不直接连接到数据库。 我想,首先经过在PHP然后用一个程序PHP在我的PHP服务器和MySQL 连接我有:?在java中的登录系统HttpURLConnection和php服务器
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$mysqli = new mysqli('localhost', 'root', '', 'persec');
if (isset($_POST['username']) and isset($_POST['password'])){
$username = $mysqli->real_escape_string($_POST['username']);
$password = trim($_POST['password']);
$pass = $mysqli->real_escape_string($_POST['password']);
$sql = "SELECT * FROM acesso WHERE username='$username'";
$result = $mysqli->query($sql);
header("Content-Type:text/plain");
http_response_code(200)
if($result->num_rows === 1){
$row = $result->fetch_array(MYSQLI_ASSOC);
if (password_verify($password, $row['password'])){
$_SESSION['username'] = $username;
echo "true";
}else{
echo "false";
}
>
,现在,我有这个在Java中
String username = txtUser.getText();
String password = new String(txtpass.getPassword());
try {
String urlParam = "u="+username+"&p="+password;
byte[] postData = urlParam.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
URL url = new URL("http://localhost/dashboard3/loginServer.php");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(postData);
System.out.println(postData);
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (IOException e1){
e1.printStackTrace();
}
我认为一切都很好,知道..问题是: 如何我可以从Java获得在PHP确认,如果我可以登录或不?
https://stackoverflow.com/questions/7570101/how-to-do-single-sign-on-sso-between-two-web-apps-php-and-java-ee也许这可以帮助您 – brianforan
提示:您可以尝试使用“HttpUrlConnection”中的[InputStream](https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getInputStream()) '读取php服务器返回的内容。 –