actually follow 3xx redirects

This commit is contained in:
jascotty2 2019-09-08 07:48:03 -05:00
parent f0abcde1b0
commit de96c9528f
2 changed files with 17 additions and 2 deletions

View File

@ -46,7 +46,7 @@ public class SongodaCore {
* Whenever we make a major change to the core GUI, updater,
* or other function used by the core, increment this number
*/
private final static int coreRevision = 3;
private final static int coreRevision = 4;
private final static int updaterVersion = 1;
private final static Set<PluginInfo> registeredPlugins = new HashSet<>();

View File

@ -1,6 +1,5 @@
package com.songoda.core.core;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.locale.Locale;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@ -35,8 +34,24 @@ public class LocaleModule implements PluginInfoModule {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setConnectTimeout(5000);
// do we need to follow a redirect?
int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) {
// get redirect url from "location" header field
String newUrl = urlConnection.getHeaderField("Location");
// get the cookie if needed
String cookies = urlConnection.getHeaderField("Set-Cookie");
// open the new connnection again
urlConnection = (HttpURLConnection) new URL(newUrl).openConnection();
urlConnection.setRequestProperty("Cookie", cookies);
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setConnectTimeout(5000);
}
Locale.saveLocale(plugin.getJavaPlugin(), urlConnection.getInputStream(), fileName);
urlConnection.disconnect();