Advertisement
Guest User

auth http

a guest
Jun 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. String userName = txtUsername.getText();
  2. String password = txtPassword.getText();
  3.  
  4. try {
  5.  
  6. String urlParameters = "username=" + userName + "&password=" + password;
  7. byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
  8. int postDataLength = postData.length;
  9. String request = "http://localhost/osnovna_sredstva/web/index.php?r=service-user/login";
  10. URL url = new URL(request);
  11. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  12. conn.setDoOutput(true);
  13. conn.setInstanceFollowRedirects(false);
  14. conn.setRequestMethod("POST");
  15. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  16. conn.setRequestProperty("charset", "utf-8");
  17. conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
  18. conn.setUseCaches(false);
  19. try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
  20. wr.write(postData);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24.  
  25. String success = "";
  26. try (DataInputStream is = new DataInputStream(conn.getInputStream())) {
  27. success = is.readLine();
  28. System.out.println(success);
  29.  
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. if ("OK".equals(success)) {
  35. RootController.password = password;
  36. RootController.username = userName;
  37. System.out.println("Sve ok");
  38. Stage stage = new Stage();
  39. Parent root = FXMLLoader.load(getClass().getResource("/osnovna_sredstva_desktop/view/MainView.fxml"));
  40. Scene scene = new Scene(root);
  41. stage.setTitle("Main form");
  42. stage.setScene(scene);
  43. ((Stage) lbglLogin.getScene().getWindow()).close();
  44. stage.show();
  45. } else {
  46. lbglLogin.setText("Wrong username or password.");
  47. }
  48. } catch (Exception ex) {
  49. ex.printStackTrace();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement