Google 安全浏览 HTTP POST - 403 响应

Google Safe Browsing HTTP POST - 403 response

我正在开发一个查询 Google 安全浏览某些 url 的程序,但我收到一个我认为不应该收到的错误。

我发送以下请求:

2
http://google.com
http://facebook.com

通过 POST 到:https://sb-ssl.google.com/safebrowsing/api/lookup?client=api&apikey=[KEY]&appver=1.5.2&pver=3.1

但是,我收到了 403 响应。

这是文档针对 HTTP POST 查找错误所说的内容:

The server generates the following HTTP error codes for the POST request:

•200: AT LEAST ONE of the queried URLs are matched in either the phishing, malware, or unwanted software lists. The actual results are returned through the response body.

•204: NONE of the queried URLs matched the phishing, malware, or unwanted software lists, and no response body is returned.

•400: Bad Request—The HTTP request was not correctly formed.

•401: Not Authorized—The API key is not authorized.

•503: Service Unavailable—The server cannot handle the request. Besides the normal server failures, this could also indicate that the client has been “throttled” for sending too many requests.

未列出响应代码 403,但我正在获取它。

我已经三重检查了我的 API-key 并确保为我的项目启用了 API。我使用的是服务器密钥,但我也尝试了浏览器密钥。

我也尝试执行 GET 请求,确实有效,但我无法使 POST 正常工作。怎么回事?

这是我的代码:

try {
   String baseURL="https://sb-ssl.google.com/safebrowsing/api/lookup";
   String arguments = "";
   arguments +=URLEncoder.encode("client", "UTF-8") + "=" + URLEncoder.encode("api", "UTF-8") + "&";
   arguments +=URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode("[KEY]", "UTF-8") + "&";
   arguments +=URLEncoder.encode("appver", "UTF-8") + "=" + URLEncoder.encode("1.5.2", "UTF-8") + "&";
   arguments +=URLEncoder.encode("pver", "UTF-8") + "=" + URLEncoder.encode("3.1", "UTF-8");

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

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

   // 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();                   

   BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));

   String decodedString;
   while ((decodedString = in.readLine()) != null) {
      System.out.println("w: " + decodedString);
   }
   in.close();
} catch(Exception e) {
   e.printStackTrace();
}

我发现了错误。 CGI 参数不正确。它应该是 key 而不是 apikey。不过你得到一个未记录的响应代码仍然很奇怪。