Files
MCCustomSkinLoader/Common/customskinloader/utils/HttpUtil0.java
T

83 lines
2.8 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import customskinloader.CustomSkinLoader;
2016-02-27 16:37:05 +08:00
public class HttpUtil0 {
2016-02-27 15:22:35 +08:00
/**
* Read all text from http.
2016-07-04 14:10:44 +08:00
* @since 13.11
2016-02-27 15:22:35 +08:00
*/
2016-07-04 14:10:44 +08:00
public static String readHttp(String url,String userAgent){
HttpURLConnection c=null;
2016-02-27 15:22:35 +08:00
try {
2016-07-31 22:00:46 +08:00
CustomSkinLoader.logger.info("Try to read '"+url+(userAgent==null?" without user agent.":"' 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()+")");
return null;
}
CustomSkinLoader.logger.info("Successfully read (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
InputStream is = c.getInputStream();
return IOUtils.toString(is, Charsets.UTF_8);
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.getMessage()+")");
return null;
} finally {
if(c!=null)
c.disconnect();
}
2016-02-27 15:22:35 +08:00
}
/**
2016-07-31 22:00:46 +08:00
* Get faked url after redirect.
2016-02-27 15:22:35 +08:00
* Using this method, can also check if it is exist.
* @param url - before redirect
2016-07-31 22:00:46 +08:00
* @return url - faked after redirect (null for not exist)
* @since 14.1
2016-02-27 15:22:35 +08:00
*/
2016-07-31 22:00:46 +08:00
public static String getFakeUrl(String url,String userAgent){
2016-07-04 14:10:44 +08:00
HttpURLConnection c=null;
2016-02-27 15:22:35 +08:00
try {
2016-07-31 22:00:46 +08:00
CustomSkinLoader.logger.info("Try to get fake 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 fake url (Response Code: "+c.getResponseCode()+")");
return null;
}
String url1=c.getURL().toString();
CustomSkinLoader.logger.info("Successfully get fake url ("
+ "Response Code: "+c.getResponseCode()+" , "
+ "Content Length: "+c.getContentLength()+" , "
+ "Real URL: "+url1+")");
return HttpTextureUtil.getLegacyFakeUrl(url1, HttpTextureUtil.getHash(url1, c.getContentLength(), c.getLastModified()));
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to get fake url (Exception: "+e.getMessage()+")");
return null;
} finally{
if(c!=null)
c.disconnect();
}
2016-02-27 15:22:35 +08:00
}
2016-07-04 14:10:44 +08:00
private static HttpURLConnection createConnection(String url,String userAgent) throws MalformedURLException, IOException{
2016-02-27 15:22:35 +08:00
HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
2016-07-31 22:00:46 +08:00
c.setReadTimeout(1000 * 10);
c.setConnectTimeout(1000 * 10);
if(userAgent!=null)
c.setRequestProperty("User-Agent",userAgent);
c.setUseCaches(false);
c.setInstanceFollowRedirects(true);
c.connect();
return c;
2016-02-27 15:22:35 +08:00
}
}