-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomDog.java
48 lines (37 loc) · 1.28 KB
/
RandomDog.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
44
45
46
47
48
package src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class RandomDog {
private static HttpURLConnection con;
public static void main(String[] args) throws
MalformedURLException,
ProtocolException,
IOException {
String url = "https://random.dog/woof.json";
try {
URL myurl = new URL(url);
con = (HttpURLConnection) myurl.openConnection();
con.setRequestMethod("GET");
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream())
)) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
}
String contentString = content.toString();
System.out.println(contentString);
} finally {
con.disconnect();
}
}
}