Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/com/germainz/crappalinks/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,30 @@ protected void onPreExecute() {
}

private String getRedirect(String url) {
HttpURLConnection c = null;
// Used for HEAD request
HttpURLConnection c1 = null;
// Used for GET request
HttpURLConnection c2 = null;
try {
c = (HttpURLConnection) new URL(url).openConnection();
c.setConnectTimeout(10000);
c.setReadTimeout(15000);
c.connect();
final int responseCode = c.getResponseCode();
c1 = (HttpURLConnection) new URL(url).openConnection();
c1.setConnectTimeout(10000);
c1.setReadTimeout(15000);
// Spare some time in case of a 3xx response code
c1.setRequestMethod("HEAD");
c1.connect();
final int responseCode = c1.getResponseCode();
// If the response code is 3xx, it's a redirection. Return the real location.
if (responseCode >= 300 && responseCode < 400) {
String location = c.getHeaderField("Location");
String location = c1.getHeaderField("Location");
return RedirectHelper.getAbsoluteUrl(location, url);
}
// It might also be a redirection using meta tags.
else if (responseCode >= 200 && responseCode < 300 ) {
Document d = Jsoup.parse(c.getInputStream(), "UTF-8", url);
c2 = (HttpURLConnection) new URL(url).openConnection();
c2.setConnectTimeout(10000);
c2.setReadTimeout(15000);
c2.connect();
Document d = Jsoup.parse(c2.getInputStream(), "UTF-8", url);
Elements refresh = d.select("*:not(noscript) > meta[http-equiv=Refresh]");
if (!refresh.isEmpty()) {
Element refreshElement = refresh.first();
Expand All @@ -104,8 +113,10 @@ else if (refreshElement.hasAttr("content") && refreshElement.attr("content").con
connectionError = true;
e.printStackTrace();
} finally {
if (c != null)
c.disconnect();
if (c1 != null)
c1.disconnect();
if (c2 != null)
c2.disconnect();
}
return null;
}
Expand Down Expand Up @@ -210,4 +221,4 @@ else if (connectionError)
}
}

}
}