Here you can find the source of queryComputeAPI(String url)
Parameter | Description |
---|---|
url | the compute API url to query |
Parameter | Description |
---|---|
IOException | an exception |
public static String queryComputeAPI(String url) throws IOException
//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); } } }