Skip to content

Commit

Permalink
修复了服务器返回的消息乱码的问题
Browse files Browse the repository at this point in the history
修复一些小bug
  • Loading branch information
RedDragon0293 committed Oct 8, 2023
1 parent 676bf41 commit d0fbbb5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 69 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
}

group 'cn.reddragon'
version '1.0-SNAPSHOT'
version '1.2'

repositories {
mavenCentral()
Expand Down
59 changes: 32 additions & 27 deletions src/main/java/cn/reddragon/eportal/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,36 @@ public static HttpURLConnection login(String username, String password, String s
out.flush();
connection.connect();
return connection;
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String getUserIndex() throws IOException {
if (!Authenticator.isOnline()) {
return null;
public static void getUserIndex() {
if (!Authenticator.getOnline()) {
userIndex = null;
}
try {
HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET");
connection.setInstanceFollowRedirects(false);
connection.connect();
Map<String, List<String>> result = connection.getHeaderFields();
String redirectLocation = result.get("Location").get(0);
userIndex = redirectLocation.substring(redirectLocation.indexOf('=') + 1);
} catch (IOException e) {
e.printStackTrace();
userIndex = null;
}
HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET");
connection.setInstanceFollowRedirects(false);
connection.connect();
Map<String, List<String>> result = connection.getHeaderFields();
String redirectLocation = result.get("Location").get(0);
String r = redirectLocation.substring(redirectLocation.indexOf('=') + 1);
userIndex = r;
return r;
}

public static HttpURLConnection logout(String userIndex) {
String content = "userIndex=" + userIndex;
//经过测试,登出时不需要userIndex也能成功登出
public static HttpURLConnection logout() {
String content = "userIndex=" + userIndex();
try {
HttpURLConnection connection = HttpUtils.make(ePortalUrl + "logout", "POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connection.setRequestProperty("Referer", "http://10.96.0.155/eportal/success.jsp?userIndex=" + userIndex);
connection.setRequestProperty("Referer", "http://10.96.0.155/eportal/success.jsp?userIndex=" + userIndex());
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(content);
out.flush();
Expand All @@ -63,7 +67,18 @@ public static HttpURLConnection logout(String userIndex) {
}
}

public static boolean isOnline() {
public static boolean getOnline() {
return online;
}

public static String userIndex() {
if (userIndex == null) {
getUserIndex();
}
return userIndex;
}

public static void checkOnline() {
try {
HttpURLConnection connection = HttpUtils.make("http://10.96.0.155/eportal/redirectortosuccess.jsp", "GET");
connection.setInstanceFollowRedirects(false);
Expand All @@ -72,14 +87,7 @@ public static boolean isOnline() {
String redirectLocation = result.get("Location").get(0);
if (Objects.equals(redirectLocation, "Http://123.123.123.123")) {
online = false;
return false;
} else if (redirectLocation.contains("http://10.96.0.155/eportal/./success.jsp?")) {
online = true;
return true;
} else {
online = false;
return false;
}
} else online = redirectLocation.contains("http://10.96.0.155/eportal/./success.jsp?");
} catch (SocketTimeoutException e) {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Expand All @@ -95,7 +103,6 @@ public static boolean isOnline() {
tray.remove(icon);
System.exit(0);
}
return false;
} catch (NoRouteToHostException e) {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Expand All @@ -111,10 +118,8 @@ public static boolean isOnline() {
tray.remove(icon);
System.exit(0);
}
return false;
} catch (IOException e) {
online = false;
return false;
}
}
}
58 changes: 29 additions & 29 deletions src/main/java/cn/reddragon/eportal/HelloApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class HelloApplication extends Application {
} catch (InterruptedException e) {
//throw new RuntimeException(e);
}
Authenticator.isOnline();
Platform.runLater(() -> {
statusLabel.setText("Current status:" + (Authenticator.online ? "Online" : "Offline"));
if (Authenticator.online) {
Authenticator.checkOnline();
statusLabel.setText("Current status:" + (Authenticator.getOnline() ? "Online" : "Offline"));
if (Authenticator.getOnline()) {
button.setText("Logout");
} else {
button.setText("Login");
Expand All @@ -37,33 +37,9 @@ public class HelloApplication extends Application {
}
});

@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 320, 240);
ChoiceBox box = (ChoiceBox) root.lookup("#selector");
box.getItems().addAll("LAN", "WAN");
box.setValue("WAN");
statusLabel = (Label) root.lookup("#statusLabel");
button = (Button) root.lookup("#button");
stage.setTitle("EPortal");
stage.setScene(scene);
stage.show();
String[] config = Config.read();
TextField name = (TextField) root.lookup("#userNameField");
TextField pass = (TextField) root.lookup("#passwordField");
name.setText(config[0]);
pass.setText(config[1]);
askThread.start();
if (!Authenticator.online) {
button.getOnAction().handle(new ActionEvent());
}
}

public static void main(String[] args) {
try {
if (Authenticator.isOnline()) {
if (Authenticator.getOnline()) {
System.out.println("Already connected!");
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Expand All @@ -83,9 +59,33 @@ public static void main(String[] args) {
} catch (NullPointerException | AWTException e) {
e.printStackTrace();
System.exit(0);
} catch (IOException ignored) {
}
launch();
System.exit(0);
}

@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 320, 240);
ChoiceBox box = (ChoiceBox) root.lookup("#selector");
box.getItems().addAll("LAN", "WAN");
box.setValue("WAN");
statusLabel = (Label) root.lookup("#statusLabel");
button = (Button) root.lookup("#button");
stage.setTitle("EPortal");
stage.setScene(scene);
stage.show();
String[] config = Config.read();
TextField name = (TextField) root.lookup("#userNameField");
TextField pass = (TextField) root.lookup("#passwordField");
name.setText(config[0]);
pass.setText(config[1]);
askThread.start();
Authenticator.checkOnline();
if (!Authenticator.getOnline()) {
button.getOnAction().handle(new ActionEvent());
}
}
}
22 changes: 13 additions & 9 deletions src/main/java/cn/reddragon/eportal/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,27 @@ public class HelloController {

@FXML
protected void onLoginButtonClick() {
if (Authenticator.online) {
if (Authenticator.getOnline()) {
if (button.getText().equals("Login")) {
resultText.setText("Already logged in!");
button.setText("Logout");
} else if (button.getText().equals("Logout")) {
try {
//经过测试,登出时不需要userIndex也能成功登出
//为了避免不必要的麻烦,仍然传入userIndex
HttpURLConnection connection = Authenticator.logout(Authenticator.userIndex);
HttpURLConnection connection = Authenticator.logout();
if (connection == null) {
resultText.setText("Error!");
return;
}
JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject();
System.out.println(resultMessage.toString());
String result = resultMessage.get("result").getAsString();
if (result.equals("success")) {
//String result = resultMessage.get("result").getAsString();
resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString());
/* if (result.equals("success")) {
resultText.setText("success!" + resultMessage.get("message").getAsString());
button.setText("Login");
} else if (result.equals("fail")) {
resultText.setText("fail!" + resultMessage.get("message").getAsString());
}
}*/
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -106,14 +105,19 @@ protected void onLoginButtonClick() {
JsonObject resultMessage = JsonParser.parseString(IOUtils.readText(loginConnection.getInputStream())).getAsJsonObject();
System.out.println(resultMessage.toString());
String result = resultMessage.get("result").getAsString();
if (result.equals("fail")) {
resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString());
if (result.equals("success")) {
Authenticator.userIndex = resultMessage.get("userIndex").getAsString();
Config.save(username, password);
}
/*if (result.equals("fail")) {
resultText.setText(resultMessage.get("message").getAsString());
} else if (result.equals("success")) {
resultText.setText("Success!" + resultMessage.get("message").getAsString());
Authenticator.userIndex = resultMessage.get("userIndex").getAsString();
button.setText("Logout");
Config.save(username, password);
}
}*/
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/cn/reddragon/eportal/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package cn.reddragon.eportal.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class IOUtils {
public static String readText(InputStream stream) {
try {
InputStreamReader reader = new InputStreamReader(stream);
StringBuilder sb = new StringBuilder();
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(reader);
return bufferedReader.readLine();
/*StringBuilder sb = new StringBuilder();
int ch = reader.read();
while (ch != -1) {
sb.append((char) ch);
ch = reader.read();
}
return sb.toString();
return sb.toString();*/
} catch (IOException e) {
e.printStackTrace();
return "";
Expand Down

0 comments on commit d0fbbb5

Please sign in to comment.