-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathValidateRecipient.java
43 lines (33 loc) · 1.58 KB
/
ValidateRecipient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// API call example. See https://www.sparkpost.com/docs/recipient-validation/integration-guide/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
class ValidateRecipient {
// Your program begins with a call to main().
// Prints "Hello, World" to the terminal window.
public static void main(final String args[]) throws Exception {
// set to api.eu.sparkpost.com for EU accounts
final String apiKey = System.getenv("SPARKPOST_API_KEY");
final String recipient = "[email protected]";
final URL url = new URL("https://api.sparkpost.com/api/v1/recipient-validation/single/" + recipient);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", apiKey);
final int code = conn.getResponseCode();
System.out.printf("%d %s\n", code, conn.getResponseMessage());
if (code >= 200 && code <= 299) {
final StringBuilder sb = new StringBuilder();
// Production code would try/catch around this
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
// Buffer the result into a string:
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
final String msg = sb.toString();
System.out.println(msg);
}
}
}