1

i had some difficulties in programming and the meaning of the Request Body is confused. It always returns 400 response codes.Please help me.

 String baseURL="https://sb-ssl.google.com/safebrowsing/api/lookup";

 String arguments = "";
 arguments+=URLEncoder.encode("client", "UTF-8")+"="+URLEncoder.encode("demo-app", "UTF-8")+"&";
 arguments+=URLEncoder.encode("apikey", "UTF-8")+"="+URLEncoder.encode("apikey", "UTF-8")+"&";
 arguments+=URLEncoder.encode("appver", "UTF-8")+"="+URLEncoder.encode("1.5.2", "UTF-8")+"&";
 arguments+=URLEncoder.encode("pver", "UTF-8")+"="+URLEncoder.encode("3.0", "UTF-8")+"&";
 arguments+=URLEncoder.encode("post_req_body", "UTF-8")+"="+URLEncoder.encode("2\nhttp://www.google.com\nhttp://www.facebook.com", "UTF-8");

 String query = arguments;

    System.out.println("Sending POST request - " + query);
// Construct the url object representing cgi script
    URL url = new URL( baseURL );

    // Get a URLConnection object, to write to POST method
    URLConnection connect = url.openConnection();

    // Specify connection settings
    connect.setDoInput(true);
    connect.setDoOutput(true);

// Get an output stream for writing
    OutputStream output = connect.getOutputStream();


    PrintStream pout = new PrintStream (output);

    pout.print ( query );
    pout.close();
  • 1
    Have you register at Google and request an API key? Your code contains neither a valid app name nor a valid API key. – Codo Dec 27 '12 at 13:48
  • In order to POST data, you will need to cast to [HttpURLConnection](http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html) and call [setRequestMethod](http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#setRequestMethod%28java.lang.String%29) – McDowell Dec 27 '12 at 13:54

1 Answers1

2

Your request is wrong. If you use a POST request, then the parameters client, apikey, apiver and pver should be part of the URL.

The request body should only consist of the URLs to check (plus the number of URLS on the first line).

So it could look like this:

String baseURL="https://sb-ssl.google.com/safebrowsing/api/lookup";

String arguments = "";
arguments + =URLEncoder.encode("client", "UTF-8") + "=" + URLEncoder.encode("myapp", "UTF-8") + "&";
arguments + =URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode("12341234", "UTF-8") + "&";
arguments + =URLEncoder.encode("appver", "UTF-8") + "=" + URLEncoder.encode("1.5.2", "UTF-8") + "&";
arguments + =URLEncoder.encode("pver", "UTF-8") + "=" + URLEncoder.encode("3.0", "UTF-8");

// Construct the url object representing cgi script
URL url = new URL(baseURL + "?" + arguments);

// Get a URLConnection object, to write to POST method
URLConnection connect = url.openConnection();

// Specify connection settings
connect.setDoInput(true);
connect.setDoOutput(true);

// Get an output stream for writing
OutputStream output = connect.getOutputStream();
PrintStream pout = new PrintStream (output);
pout.print("2");
pout.println();
pout.print("http://www.google.com");
pout.println();
pout.print("http://www.facebook.com");
pout.close();
Codo
  • 75,595
  • 17
  • 168
  • 206
  • What more do we need to do in order to send one URL to google api, and receive an answer wether its safe or not ? I think the first part is done with the above code, and next we need to wait for an answer? – Giannis Feb 27 '13 at 22:49