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

byte[]getBytes(String urlStr)
read url into byte[]
try {
    URL u = new URL(urlStr);
    URLConnection uc = u.openConnection();
    InputStream is = uc.getInputStream();
    byte[] buf = new byte[4096];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (true) {
        int read = is.read(buf);
...
StringgetCharset(URLConnection connection)
get Charset
String contentType = connection == null ? null : connection.getContentType();
if (contentType != null) {
    StringTokenizer tok = new StringTokenizer(contentType, ";");
    if (tok.hasMoreTokens()) {
        tok.nextToken();
        while (tok.hasMoreTokens()) {
            String assignment = tok.nextToken().trim();
            int eqIdx = assignment.indexOf('=');
...
char[]getCharsFromURL(URL url)
Reads the content of a URL as a char array using the default connect and read timeouts.
return getTextFromURL(url, defaultConnectTimeout(), defaultReadTimeout()).toCharArray();
ListgetChildren(URL url)
Takes a given url and creates a list which contains all children of the given url.
List<String> result = new ArrayList<String>();
if ("file".equals(url.getProtocol())) {
    File file = new File(url.getPath());
    if (!file.isDirectory()) {
        file = file.getParentFile();
    addFiles(file, result, file);
} else if ("jar".equals(url.getProtocol())) {
...
URLConnectiongetConnection(URL url)
get Connection
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.connect();
return connection;
StringgetConnectionResponseHeaders(URLConnection c)
get Connection Response Headers
int i = 0;
String value;
StringBuilder sb = new StringBuilder();
while ((value = c.getHeaderField(i)) != null) {
    String key = c.getHeaderFieldKey(i);
    sb.append(key);
    sb.append(": ");
    sb.append(value);
...
URLgetContainerUrl(URL url, String resourceInThatDir)
get Container Url
boolean isJar = "jar".equals(url.getProtocol());
if (isJar) {
    try {
        JarURLConnection connection = (JarURLConnection) url.openConnection();
        url = connection.getJarFileURL();
    } catch (IOException e) {
        throw new IllegalStateException(e);
} else {
    String path = url.toString();
    int i = path.indexOf(resourceInThatDir);
    if (i == -1)
        throw new IllegalStateException(
                "Resource path (" + resourceInThatDir + ") not in url substring (" + url + ")");
    String parent = path.substring(0, i);
    try {
        url = new URL(parent);
    } catch (MalformedURLException e) {
        throw new IllegalStateException(
                "Resource (" + resourceInThatDir + ") found at invalid URL parent (" + parent + ")", e);
return url;
voidgetCookies(URLConnection conn, Map> store)
Retrieves and stores cookies returned by the host on the other side of the open java.net.URLConnection.
String domain = getCookieDomainFromHost(conn.getURL().getHost());
Map<String, ConcurrentHashMap<?, ?>> domainStore; 
if (store.containsKey(domain)) {
    domainStore = (Map<String, ConcurrentHashMap<?, ?>>) store.get(domain);
} else {
    domainStore = new ConcurrentHashMap<>();
    store.put(domain, domainStore);
if (domainStore.containsKey("JSESSIONID")) {
    return;
String headerName;
for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
    if (headerName.equalsIgnoreCase(SET_COOKIE)) {
        ConcurrentHashMap<String, String> cookie = new ConcurrentHashMap<>();
        StringTokenizer st = new StringTokenizer(conn.getHeaderField(i), COOKIE_VALUE_DELIMITER);
        if (st.hasMoreTokens()) {
            String token = st.nextToken();
            String key = token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR)).trim();
            String value = token.substring(token.indexOf(NAME_VALUE_SEPARATOR) + 1);
            domainStore.put(key, cookie);
            cookie.put(key, value);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            int pos = token.indexOf(NAME_VALUE_SEPARATOR);
            if (pos != -1) {
                String key = token.substring(0, pos).toLowerCase().trim();
                String value = token.substring(token.indexOf(NAME_VALUE_SEPARATOR) + 1);
                cookie.put(key, value);
StringgetDataFromServer(URL url)
get Data From Server
java.net.URLConnection conn = url.openConnection();
StringBuffer sb = new StringBuffer();
conn.setDoInput(true);
conn.setDoOutput(false);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
    if (line.trim().length() > 0) {
...
URLConnectiongetDefaultUrlConnection(URL url)
get Default Url Connection
final URLConnection connection = (URLConnection) url.openConnection();
connection.setRequestProperty("User-Agent",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
return connection;