Added custom useragent

Faster mod loading
Backup broken json
Added a new skin site
This commit is contained in:
Alexander Xia committed 2016-07-04 14:10:44 +08:00
1 parent 4e78285bbe
commit 7654dc583d
9 files changed
+57 -20

No files matched your search

+19 -8
View File
@@ -14,11 +14,13 @@ import customskinloader.CustomSkinLoader;
public class HttpUtil0 {
/**
* Read all text from http.
* @since 13.11
*/
public static String readHttp(String url){
public static String readHttp(String url,String userAgent){
HttpURLConnection c=null;
try {
CustomSkinLoader.logger.info("Try to read '"+url+"'");
HttpURLConnection c=createConnection(url);
CustomSkinLoader.logger.info("Try to read '"+url+"' with user agent '"+userAgent+"'.");
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5||c.getResponseCode()==HttpURLConnection.HTTP_NO_CONTENT) {
CustomSkinLoader.logger.info("Failed to read (Response Code: "+c.getResponseCode()+")");
@@ -30,6 +32,9 @@ public class HttpUtil0 {
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.getMessage()+")");
return null;
} finally {
if(c!=null)
c.disconnect();
}
}
@@ -38,11 +43,13 @@ public class HttpUtil0 {
* Using this method, can also check if it is exist.
* @param url - before redirect
* @return url - after redirect (null for not exist)
* @since 13.11
*/
public static String getRealUrl(String url){
public static String getRealUrl(String url,String userAgent){
HttpURLConnection c=null;
try {
CustomSkinLoader.logger.info("Try to get real url of '"+url+"'.");
HttpURLConnection c=createConnection(url);
CustomSkinLoader.logger.info("Try to get real url of '"+url+"' with user agent '"+userAgent+"'.");
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5) {
CustomSkinLoader.logger.info("Failed to get real url (Response Code: "+c.getResponseCode()+")");
@@ -56,13 +63,17 @@ public class HttpUtil0 {
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to get real url (Exception: "+e.getMessage()+")");
return null;
} finally{
if(c!=null)
c.disconnect();
}
}
private static HttpURLConnection createConnection(String url) throws MalformedURLException, IOException{
private static HttpURLConnection createConnection(String url,String userAgent) throws MalformedURLException, IOException{
HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
c.setReadTimeout(1000 * 10);
c.setConnectTimeout(1000 * 10);
if(userAgent!=null)
c.setRequestProperty("User-Agent",userAgent);
c.setUseCaches(false);
c.setInstanceFollowRedirects(true);
c.connect();