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

StringgetUrlSource(String url)
get Url Source
URL yahoo = new URL(url);
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    a.append(inputLine + "\n");
in.close();
return a.toString();
InputStreamgetURLStream(URL url, int level)
Get a stream to a URL accommodating possible redirections.
if (level > 5) {
    throw new IOException("Two many levels of redirection in URL");
URLConnection conn = url.openConnection();
Map hdrs = conn.getHeaderFields();
String[] keys = (String[]) hdrs.keySet().toArray(new String[0]);
for (String key : keys) {
    if (key != null && key.toLowerCase().equals("location")) {
...
StringgetWebPageHtmlContent(String url)
get Web Page Html Content
String line;
try (final BufferedReader is = sendRequest(new URL(url))) {
    try (final StringWriter os = new StringWriter()) {
        while ((line = is.readLine()) != null) {
            os.append(line);
        return os.toString();
} catch (MalformedURLException e) {
    throw new MalformedURLException("Invalid URL " + url);
} catch (IOException e) {
    throw new IOException("Error trying to write in the local buffer to store the page HTML from " + url,
            e);
ArrayListgetWebsiteContents(URL url)
get Website Contents
ArrayList fileContents = new ArrayList();
URLConnection openConnection = url.openConnection();
openConnection.addRequestProperty("User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
BufferedReader fileReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream()));
String fileLine = "";
while ((fileLine = fileReader.readLine()) != null) {
    if (!fileLine.equals("")) {
...
ListhttpPost(String urlString, String postPath, Map keyValuePairs)
http Post
List<String> ret = new ArrayList<String>();
try {
    String firstPart;
    if (urlString.substring(urlString.length() - 1).compareTo("/") == 0) {
        firstPart = urlString.substring(0, urlString.length() - 1);
    } else {
        firstPart = urlString;
    String secondPart;
    if (postPath.substring(0).compareTo("/") == 0) {
        secondPart = postPath.substring(1, postPath.length() - 1);
    } else {
        secondPart = postPath.substring(0, postPath.length() - 1);
    String completePath = firstPart + "/" + secondPart;
    URL url = new URL(completePath);
    String data = null;
    for (Entry<String, String> entry : keyValuePairs.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (data == null) {
            data = URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
        } else {
            data += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        ret.add(line);
    wr.close();
    rd.close();
} catch (Exception e) {
    return null;
return ret;
InputStreamimageFromUrl(String url)
image From Url
if (url == null)
    return null;
try {
    URL u = new URL(url);
    URLConnection urlConnection = u.openConnection();
    urlConnection.setRequestProperty("user-agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
    return urlConnection.getInputStream();
...
booleanisJarDirectory(JarURLConnection conn)
is Jar Directory
JarFile jarFile = conn.getJarFile();
String entryName = conn.getEntryName();
ZipEntry dirEntry = jarFile.getEntry(addEndingSlash(entryName));
return dirEntry != null && dirEntry.isDirectory();
booleanisOnline(String url)
Verifies if the specified URL is reachable online.
try {
    final URLConnection connection = new URL(url).openConnection();
    connection.getInputStream().close();
    return true;
} catch (IOException ioe) {
    if (ioe instanceof MalformedURLException) {
        throw (MalformedURLException) ioe;
    return false;
booleanisUrlReachable(String url)
is Url Reachable
try {
    final URLConnection connection = new URL(url).openConnection();
    connection.setConnectTimeout(CONNECT_TIMEOUT);
    connection.connect();
    return true;
} catch (final IOException e) {
    return false;
booleanisValidToc(URL url)
is Valid Toc
InputStream in = null;
try {
    URLConnection connection = url.openConnection();
    setTimeout(connection, SOCKET_TIMEOUT);
    connection.connect();
    in = connection.getInputStream();
    if (in != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
...