Java Utililty Methods URL Connection

List of utility methods to do URL Connection

Description

The list of methods to do URL Connection are organized into topic(s).

Method

StringdoGet(String url, String charset)
doGet
URL reqUrl = null;
URLConnection conn = null;
BufferedReader in = null;
StringBuffer result = new StringBuffer();
try {
    reqUrl = new URL(url);
    conn = reqUrl.openConnection();
    conn.setConnectTimeout(10000);
...
InputStreamdoPost(final URL url, final Map parameters, final boolean encode)
do Post
final URLConnection urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
OutputStream os = null;
try {
    os = new DataOutputStream(urlConn.getOutputStream());
...
StringdoPost(String url, String params, String charset)
doPost
URL reqUrl = null;
URLConnection conn = null;
OutputStreamWriter out = null;
BufferedWriter bw = null;
OutputStream os = null;
BufferedReader in = null;
StringBuffer result = new StringBuffer();
try {
...
List>findClasses(URL resource, String packageName, boolean annotated)
Recursive method used to find all classes in a given directory and subdirs.
List<Class<?>> classes = new ArrayList<Class<?>>();
File directory = new File(resource.getFile());
if (directory.exists()) {
    findClassInDir(directory, packageName, classes, annotated);
} else {
    JarURLConnection conn = (JarURLConnection) resource.openConnection();
    JarFile jarFile = conn.getJarFile();
    Enumeration<JarEntry> e = jarFile.entries();
...
SetfindClasspathUrls(ClassLoader classLoader)
find Classpath Urls
Set<URL> urls = new HashSet<URL>();
try {
    String[] classpathProperties = System.getProperty("java.class.path").split(File.pathSeparator);
    for (String classpathProperty : classpathProperties) {
        urls.add(new File(classpathProperty).toURI().toURL());
    urls.addAll(getExtURLs(getExtDirs()));
} catch (IOException e) {
...
voidfindResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
find Resource In Jar Package
JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    String name = entry.getName();
    if (name.charAt(0) == '/')
        name = name.substring(1);
    if (name.startsWith(packageDirName)) {
...
InputStreamgetAsStream(URL url)
get As Stream
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
return conn.getInputStream();
InputStreamgetBaseAuthInputStreamFromURL(String query, String basicAuthString)
get Base Auth Input Stream From URL
URL url = new URL(query);
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", basicAuthString);
return uc.getInputStream();
StringgetBaseURL(URLConnection conn)
get the base URL from a connection
String url = conn.getURL().toExternalForm();
return getBaseURL(url);
booleangetBooleanFromUrl(String url)
get Boolean From Url
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
in.close();
if (response.toString().equals("true")) {
    return true;
} else {
    return false;