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服務器返回的內容。 –