mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-02 16:49:10 +00:00
Added web utils
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package org.parabot.core;
|
||||
|
||||
public class WebConstants {
|
||||
public static final String HOME = "http://parnassian.host56.com/";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package org.parabot.environment.api.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Clisprail
|
||||
*
|
||||
*/
|
||||
public class WebUtil {
|
||||
private static String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
|
||||
/**
|
||||
* Agent to set at a URL connection
|
||||
* @param userAgent
|
||||
*/
|
||||
public static void setUserAgent(final String userAgent) {
|
||||
agent = userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets useragent
|
||||
* @return useragent
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
return agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches content of a page
|
||||
* @param location
|
||||
* @return contents of page
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public static String getContents(final String location)
|
||||
throws MalformedURLException {
|
||||
return getContents(new URL(location));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contents from URL
|
||||
* @param url
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(final URL url) {
|
||||
try {
|
||||
final BufferedReader in = getReader(url);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
in.close();
|
||||
return builder.toString();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets BufferedReader from URL
|
||||
* @param url
|
||||
* @return BufferedReader from URL
|
||||
*/
|
||||
public static BufferedReader getReader(final URL url) {
|
||||
try {
|
||||
final URLConnection con = getConnection(url);
|
||||
return new BufferedReader(new InputStreamReader(
|
||||
con.getInputStream()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a connection
|
||||
* @param url
|
||||
* @return URLConnection to URL
|
||||
*/
|
||||
public static URLConnection getConnection(final URL url) {
|
||||
try {
|
||||
final URLConnection con = url.openConnection();
|
||||
con.setRequestProperty("User-Agent", agent);
|
||||
return con;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user