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

InputStreamparseURL(ClassLoader classLoader, String uri)
parse URL
URL url = null;
InputStream input = null;
if (uri.startsWith("http://") || uri.startsWith("file:///")) {
    url = new URL(uri);
    URLConnection connection = url.openConnection();
    input = connection.getInputStream();
} else {
    url = classLoader.getResource(uri);
...
voidpingUrl(URL url)
ping Url
URLConnection con = url.openConnection();
con.connect();
con.getInputStream().read();
StringqueryComputeAPI(String url)
Queries the compute api on the url specified and returns the response.
try {
    URLConnection computeIdUrlConnection = new URL(url).openConnection();
    computeIdUrlConnection.addRequestProperty("Metadata-Flavor", "Google");
    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(computeIdUrlConnection.getInputStream()))) {
        return reader.readLine(); 
} catch (IOException e) {
...
voidrenderRequest(URLConnection connection)
Render request according setting
if (connectTimeout != null) {
    connection.setConnectTimeout(connectTimeout);
if (socketTimeout != null) {
    connection.setReadTimeout(socketTimeout);
ListresolveModuleEntriesFromJar(URL url, String _prefix)
resolve Module Entries From Jar
final String prefix = _prefix.endsWith("/") ? _prefix : _prefix + "/";
List<String> resourceList = new ArrayList<String>();
JarURLConnection conn = (JarURLConnection) url.openConnection();
Enumeration entries = conn.getJarFile().entries();
while (entries.hasMoreElements()) {
    JarEntry entry = (JarEntry) entries.nextElement();
    String name = entry.getName();
    if (name.startsWith(prefix) && !entry.isDirectory()) {
...
StringretrieveData(URL url)
Opens a connection to the given URL (typically HTTP) and retrieves the data from server.
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-agent", "MZmine 3");
InputStream is = connection.getInputStream();
if (is == null) {
    throw new IOException("Could not establish a connection to " + url);
StringBuffer buffer = new StringBuffer();
try {
...
StringretrieveFromUrl(String url, boolean useCache, boolean deleteOnExit)
retrieve From Url
System.gc();
String buffFile = getTempDir().getAbsolutePath() + "\\" + toUniqueFileName(url);
String text = null;
if (useCache) {
    try {
        text = fileToString(buffFile);
    } catch (IOException e) {
    if (text != null) {
        return text;
StringBuffer pageText = new StringBuffer();
URL location = new URL(url);
URLConnection yc = location.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
    pageText.append(inputLine);
in.close();
if (useCache) {
    if (VERBOSE)
        System.out.println("Caching URL.. " + url);
    stringToFile(pageText.toString(), buffFile, false, deleteOnExit);
return pageText.toString();
StringretrieveHtml(URLConnection http)
Returns the text received from a web post
StringBuffer htmlOutput = new StringBuffer();
InputStreamReader input = new InputStreamReader(http.getInputStream());
BufferedReader inStream = new BufferedReader(input);
String inputLine;
while ((inputLine = inStream.readLine()) != null) {
    htmlOutput.append(inputLine + System.getProperty("line.separator"));
inStream.close();
...
voidscanPackageByJar(List> classes, final String packageName, final URL jarUrl, final boolean recursive)
scan Package By Jar
String packagePath = packageName.replace(".", "/");
try {
    JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile();
    Enumeration<JarEntry> entrys = jarFile.entries();
    while (entrys.hasMoreElements()) {
        JarEntry jarEntry = entrys.nextElement();
        String entryName = jarEntry.getName();
        if (!entryName.startsWith(packagePath) || jarEntry.isDirectory()) {
...
StringsendGetRequest(String resourceUrl, String contentType)
Send an HTTP GET request to the given url
String response = mCache.get(resourceUrl);
if (response == null) {
    response = "";
    if (resourceUrl.startsWith("http://")) {
        try {
            URL url = new URL(resourceUrl);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Content-type", contentType + "; charset=UTF-8");
...