Advertisement
Suzana_Marek

endpoint_emailValidation

Nov 20th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package org.example;
  2.  
  3. import java.io.OutputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.util.Scanner;
  7.  
  8. class emailValidator {
  9.  
  10.     public static void main(String[] args) {
  11.         String apiUrl = "https://dev.dripit.io/api/is-email-used";
  12.         String email = "[email protected]";
  13.  
  14.         boolean isEmailUsed = validateEmail(apiUrl, email);
  15.  
  16.         if (isEmailUsed) {
  17.             System.out.println("The email is already used.");
  18.         } else {
  19.             System.out.println("The email is available.");
  20.         }
  21.     }
  22.  
  23.     public static boolean validateEmail(String apiUrl, String email) {
  24.         try {
  25.  
  26.             URL url = new URL(apiUrl);
  27.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  28.  
  29.             connection.setRequestMethod("POST");
  30.             connection.setRequestProperty("Content-Type", "application/json");
  31.             connection.setDoOutput(true);
  32.  
  33.             String requestBody = "{\"email\":\"" + email + "\"}";
  34.             OutputStream outputStream = connection.getOutputStream();
  35.             outputStream.write(requestBody.getBytes());
  36.             outputStream.flush();
  37.             outputStream.close();
  38.  
  39.             int responseCode = connection.getResponseCode();
  40.             if (responseCode == 201) {
  41.                 Scanner scanner = new Scanner(connection.getInputStream());
  42.                 String response = scanner.useDelimiter("\\A").next();
  43.                 scanner.close();
  44.  
  45.                 return Boolean.parseBoolean(response);
  46.             } else {
  47.                 System.out.println("Error: Server returned status code " + responseCode);
  48.             }
  49.  
  50.         } catch (Exception e) {
  51.             System.out.println("An error occurred: " + e.getMessage());
  52.         }
  53.  
  54.         return false;
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement