Skip to content

Commit d977444

Browse files
Added changes_4
1 parent 3cdfc81 commit d977444

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

app/src/main/java/hexlet/code/controller/UrlChecksController.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,25 @@ public static void check(Context ctx) throws SQLException {
2727
.asString();
2828

2929
int statusCode = response.getStatus();
30+
String body = response.getBody();
31+
32+
if (statusCode >= 300 && statusCode < 400) {
33+
String redirectUrl = response.getHeaders().getFirst("Location");
34+
if (redirectUrl != null && !redirectUrl.isEmpty()) {
35+
response = Unirest.get(redirectUrl)
36+
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
37+
.asString();
38+
statusCode = response.getStatus();
39+
body = response.getBody();
40+
}
41+
}
42+
3043
String h1 = "";
3144
String title = "";
3245
String description = "";
3346

34-
if (statusCode == 200) {
35-
var doc = Jsoup.parse(response.getBody());
47+
if (body != null && !body.isEmpty()) {
48+
var doc = Jsoup.parse(body);
3649
var h1El = doc.selectFirst("h1");
3750
var descEl = doc.selectFirst("meta[name~=(?i)description]");
3851

0 commit comments

Comments
 (0)