Java URL Connection queryComputeAPI(String url)

Here you can find the source of queryComputeAPI(String url)

Description

Queries the compute api on the url specified and returns the response.

License

Open Source License

Parameter

Parameter Description
url the compute API url to query

Exception

Parameter Description
IOException an exception

Return

the response

Declaration

public static String queryComputeAPI(String url) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**// w  w w . j ava2s.c  o  m
     * Queries the compute api on the url specified and returns the response.
     * A lot of the API responses are values that never change and therefore should be cached where possible.
     *
     * @param url the compute API url to query
     * @return the response
     * @throws IOException
     */
    public static String queryComputeAPI(String url) throws IOException {
        try {
            URLConnection computeIdUrlConnection = new URL(url).openConnection();
            computeIdUrlConnection.addRequestProperty("Metadata-Flavor", "Google");

            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(computeIdUrlConnection.getInputStream()))) {
                return reader.readLine(); // compute is single line response
            }
        } catch (IOException e) {
            throw new IOException("Error retrieving response from Google Compute API.", e);
        }
    }
}

Related

  1. loadLinesUrl(String url)
  2. loadObjectFromURL(URL url)
  3. map2URL(final Properties properties)
  4. parseURL(ClassLoader classLoader, String uri)
  5. pingUrl(URL url)
  6. renderRequest(URLConnection connection)
  7. resolveModuleEntriesFromJar(URL url, String _prefix)
  8. retrieveData(URL url)
  9. retrieveFromUrl(String url, boolean useCache, boolean deleteOnExit)