List of utility methods to do URL Connection
String | getUrlSource(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(); |
InputStream | getURLStream(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")) { ... |
String | getWebPageHtmlContent(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); |
ArrayList | getWebsiteContents(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("")) { ... |
List | httpPost(String urlString, String postPath, Map 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; |
InputStream | imageFromUrl(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(); ... |
boolean | isJarDirectory(JarURLConnection conn) is Jar Directory JarFile jarFile = conn.getJarFile();
String entryName = conn.getEntryName();
ZipEntry dirEntry = jarFile.getEntry(addEndingSlash(entryName));
return dirEntry != null && dirEntry.isDirectory();
|
boolean | isOnline(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; |
boolean | isUrlReachable(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; |
boolean | isValidToc(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)); ... |