Thanks, I am trying to consume the google custom search REST API with the following java code. I get an exception that says "bad request". What am I doing wrong? Here is the google api documentation I have been following:
https://developers.google.com/custom...etting_started
Thanks in advance.
package googlerest;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;
public class GoogleREST {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try {
String result = httpGet("https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=SEARCHENGINE_UNIQUEID");
System.out.println(result);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public static String httpGet(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
return sb.toString();
}
}