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

booleanisValidUrl(String input)
is Valid Url
if (!input.startsWith("http://") && !input.startsWith("https://")) {
    input = ("http://" + input);
try {
    URI uri = new URI(input);
    URL url = uri.toURL();
    java.net.URLConnection conn = url.openConnection();
    conn.connect();
...
booleanisZipName(URI documentIRI, URLConnection connection)
is Zip Name
if (isZipFileName(documentIRI.toString())) {
    return true;
} else {
    String fileName = getFileNameFromContentDisposition(connection);
    return fileName != null && isZipFileName(fileName);
ListjarURLDirectories(URL jarURL)
Return the directories in a jar URI, relative to the jar entry, if any.
List directories = new LinkedList();
JarURLConnection connection = (JarURLConnection) (jarURL.openConnection());
String jarEntryName = connection.getEntryName();
if (jarEntryName.endsWith("/")) {
    jarEntryName = jarEntryName.substring(0, jarEntryName.length() - 1);
JarFile jarFile = connection.getJarFile();
Enumeration entries = jarFile.entries();
...
URLjarURLEntryResource(String jarURLString)
Lookup a jar URL and return the resource.
int jarEntry = jarURLString.indexOf("!/");
if (jarEntry == -1) {
    jarEntry = jarURLString.indexOf("!\\");
    if (jarEntry == -1) {
        return null;
try {
...
longlastModifiedURL(String urlstr)
last Modified URL
long result = 0L;
if (urlstr != null) {
    try {
        URL url = new URL(urlstr);
        URLConnection conn = url.openConnection();
        result = conn.getLastModified();
    } catch (IOException ex) {
        ex.printStackTrace();
...
StringloadCookie(URL url)
load Cookie
URLConnection conn = url.openConnection();
conn.connect();
Map<String, List<String>> headers = conn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
if (values.size() > 0) {
    return values.get(0);
return "";
...
ListloadJar(String path, URL resource)
load Jar
JarURLConnection conn = (JarURLConnection) resource.openConnection();
JarFile jarFile = conn.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
List<URL> result = new LinkedList<URL>();
String p = path;
if (p.endsWith("/") == false) {
    p = p + "/";
while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    if ((!entry.getName().equals(p))
            && (entry.getName().startsWith(p) || entry.getName().startsWith("WEB-INF/classes/" + p))) {
        URL url = new URL("jar:" + new URL("file", null, jarFile.getName() + "!/" + entry.getName()));
        result.add(url);
return result;
ListloadLinesUrl(String url)
Load the lines from a remote URL into a list of strings.
try {
    URL resource = new URL(url);
    String filename = resource.getFile().replaceAll("/", "_");
    URLConnection conn = resource.openConnection();
    File file = new File(filename);
    if (!file.exists() || file.lastModified() < conn.getLastModified()) {
        byte buffer[] = new byte[4096];
        int n;
...
ObjectloadObjectFromURL(URL url)
return a object read from the URL
try {
    URLConnection uc = url.openConnection();
    ObjectInputStream tis = new ObjectInputStream((InputStream) uc.getContent());
    Object obj = tis.readObject();
    tis.close();
    return obj;
} catch (Exception e) {
    e.printStackTrace();
...
URLmap2URL(final Properties properties)
map URL
URLStreamHandler handler = new URLStreamHandler() {
    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        return new URLConnection(u) {
            @Override
            public void connect() throws IOException {
            @Override
...