Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package ph.activelearning.quicktexter;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8.  
  9. /**
  10.  * Created by Admin on 03/17/2017.
  11.  */
  12.  
  13. public class Authenticator {
  14.     final String TARGET_URL = "http://www.activelearning.ph/quicktexter/getUserIdByUserName.php";
  15.  
  16.     public int getUserId(String username, String password) {
  17.         HttpURLConnection urlConnection = null;
  18.         int userId = -1;
  19.  
  20.         try {
  21.             URL url = new URL(TARGET_URL + "?username=" +
  22.                     username + "&password=" + password);
  23.  
  24.             urlConnection = (HttpURLConnection) url.openConnection();
  25.  
  26.             if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  27.                 InputStream inStream = urlConnection.getInputStream();
  28.                 BufferedReader in = new BufferedReader(
  29.                         new InputStreamReader(inStream));
  30.                 String inLine = null;
  31.                 while ((inLine = in.readLine()) != null) {
  32.                     userId = Integer.parseInt(inLine);
  33.                 }
  34.                 in.close();
  35.             }
  36.  
  37.         } catch (Exception e) {
  38.             e.printStackTrace();
  39.         }
  40.         return userId;
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.         Authenticator auth = new Authenticator();
  45.         System.out.println("User ID: " + auth.getUserId("a", "a"));
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement