Skip to content

Commit 211eeed

Browse files
authored
rejoice, ye heavens, and ye that dwell in them
Therefore rejoice, ye heavens, and ye that dwell in them. Woe to the inhabiters of the earth and of the sea! for the devil is come down unto you, having great wrath, because he knoweth that he hath but a short time. (Revelation 12:12)
1 parent f5e5d72 commit 211eeed

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
//Therefore rejoice, ye heavens, and ye that dwell in them. Woe to the inhabiters of the earth and of the sea!
3+
//for the devil is come down unto you, having great wrath, because he knoweth that he hath but a short time. (Revelation 12:12)
4+
5+
package com.javarush.task.task40.task4010;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
14+
/*
15+
Коды ошибок
16+
*/
17+
18+
public class Solution {
19+
public static void main(String[] args) {
20+
try {
21+
URL url = new URL("http://jsonplaceholder.typicode.com/posts/1");
22+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
23+
conn.setRequestMethod("GET");
24+
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
25+
26+
if (conn.getResponseCode() != 200) {
27+
throw new RuntimeException("Failed : HTTP error code : "
28+
+ conn.getResponseCode());
29+
}
30+
31+
BufferedReader br = new BufferedReader(new InputStreamReader(
32+
(conn.getInputStream())));
33+
34+
String output;
35+
System.out.println("Output from Server .... \n");
36+
while ((output = br.readLine()) != null) {
37+
System.out.println(output);
38+
}
39+
40+
conn.disconnect();
41+
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
47+
48+
/*
49+
Коды ошибок
50+
51+
В методе main присутствуют ошибки. Исправь их. Постарайся сделать минимум изменений.
52+
53+
54+
55+
Результатом работы программы должно быть отображение JSON документа по ссылке url.
56+
57+
58+
59+
60+
61+
Требования:
62+
63+
1. У соединения должен быть корректно установлен параметр User-Agent с помощью метода setRequestProperty.
64+
65+
2. В случае, если код ответа не равен 200, должно быть выброшено исключение RuntimeException.
66+
67+
3. В случае, если код ответа равен 200, на экран должна быть выведена информация полученная из InputStream соединения.
68+
69+
4. В методе main должен быть создан новый объект типа URL.
70+
*/

0 commit comments

Comments
 (0)