From 439722d2e45a2ea941bc95ff0d7d850f47155ff0 Mon Sep 17 00:00:00 2001 From: 4pr0n Date: Sun, 11 Jan 2015 00:28:41 -0800 Subject: [PATCH] Send useragent and referer when downloading videos - fixes gfycat ripper --- .../ripme/ripper/DownloadVideoThread.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/DownloadVideoThread.java b/src/main/java/com/rarchives/ripme/ripper/DownloadVideoThread.java index 665f307b..8e4d1e56 100644 --- a/src/main/java/com/rarchives/ripme/ripper/DownloadVideoThread.java +++ b/src/main/java/com/rarchives/ripme/ripper/DownloadVideoThread.java @@ -9,6 +9,8 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import javax.net.ssl.HttpsURLConnection; + import org.apache.log4j.Logger; import com.rarchives.ripme.ui.RipStatusMessage.STATUS; @@ -78,8 +80,24 @@ public class DownloadVideoThread extends Thread { try { logger.info(" Downloading file: " + url + (tries > 0 ? " Retry #" + tries : "")); observer.sendUpdate(STATUS.DOWNLOAD_STARTED, url.toExternalForm()); + + // Setup HTTP request + HttpURLConnection huc; + if (this.url.toString().startsWith("https")) { + huc = (HttpsURLConnection) this.url.openConnection(); + } + else { + huc = (HttpURLConnection) this.url.openConnection(); + } + huc.setInstanceFollowRedirects(true); + huc.setConnectTimeout(0); // Never timeout + huc.setRequestProperty("accept", "*/*"); + huc.setRequestProperty("Referer", this.url.toExternalForm()); // Sic + huc.setRequestProperty("User-agent", AbstractRipper.USER_AGENT); tries += 1; - bis = new BufferedInputStream(this.url.openStream()); + huc.connect(); + // Check status code + bis = new BufferedInputStream(huc.getInputStream()); fos = new FileOutputStream(saveAs); while ( (bytesRead = bis.read(data)) != -1) { try { @@ -120,6 +138,9 @@ public class DownloadVideoThread extends Thread { private int getTotalBytes(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("Referer", this.url.toExternalForm()); // Sic + conn.setRequestProperty("User-agent", AbstractRipper.USER_AGENT); return conn.getContentLength(); }