Advertisement
Suzana_Marek

emailChecker

Oct 21st, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import javax.net.ssl.HttpsURLConnection;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStream;
  4. import java.net.URL;
  5. import java.nio.charset.StandardCharsets;
  6. import java.util.Scanner;
  7. import java.io.BufferedReader;
  8.  
  9. public class emailChecker {
  10.     public static class EmailChecker {
  11.         public static void main(String[] args) {
  12.             Scanner scanner = new Scanner(System.in);
  13.  
  14.             try (scanner) {
  15.                 System.out.print("Enter the email to check: ");
  16.                 String emailToCheck = scanner.nextLine();
  17.                 String endpoint = "https://dev.dripit.io/api/is-email-used";
  18.                 URL url = new URL(endpoint);
  19.                 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  20.                 connection.setRequestMethod("POST");
  21.  
  22.                 connection.setRequestProperty("Content-Type", "application/json");
  23.                 connection.setDoOutput(true);
  24.  
  25.                 String jsonInputString = "{\"email\":\"" + emailToCheck + "\"}";
  26.                 try (OutputStream os = connection.getOutputStream()) {
  27.                     byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
  28.                     os.write(input, 0, input.length);
  29.                 }
  30.                 int responseCode = connection.getResponseCode();
  31.                 System.out.println("Response: " + responseCode);
  32.  
  33.                 try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
  34.                     StringBuilder response = new StringBuilder();
  35.                     String responseLine;
  36.                     while ((responseLine = br.readLine()) != null) {
  37.                         response.append(responseLine.trim());
  38.                     }
  39.                     System.out.println("Response: " + response);
  40.                 }
  41.             } catch (Exception e) {
  42.                 e.printStackTrace();
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement