Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Scanner;
- class emailValidator {
- public static void main(String[] args) {
- String apiUrl = "https://dev.dripit.io/api/is-email-used";
- boolean isEmailUsed = validateEmail(apiUrl, email);
- if (isEmailUsed) {
- System.out.println("The email is already used.");
- } else {
- System.out.println("The email is available.");
- }
- }
- public static boolean validateEmail(String apiUrl, String email) {
- try {
- URL url = new URL(apiUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setDoOutput(true);
- String requestBody = "{\"email\":\"" + email + "\"}";
- OutputStream outputStream = connection.getOutputStream();
- outputStream.write(requestBody.getBytes());
- outputStream.flush();
- outputStream.close();
- int responseCode = connection.getResponseCode();
- if (responseCode == 201) {
- Scanner scanner = new Scanner(connection.getInputStream());
- String response = scanner.useDelimiter("\\A").next();
- scanner.close();
- return Boolean.parseBoolean(response);
- } else {
- System.out.println("Error: Server returned status code " + responseCode);
- }
- } catch (Exception e) {
- System.out.println("An error occurred: " + e.getMessage());
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement