java - Google 安全浏览 HTTP POST - 403 响应

标签 java google-api safe-browsing

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

我正在发送以下请求:

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。我正在使用服务器 key ,但我也尝试了浏览器 key 。

我也尝试执行 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。不过,你得到一个未记录的响应代码仍然很奇怪。

关于java - Google 安全浏览 HTTP POST - 403 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182690/

相关文章:

java - 如何将我的 Eclipse 项目转换为较早的 Java 版本?

html - 通过 HTML5 和 Google Text API 重新创建文本效果

google-api - 努力为 AdSense API 存储 token

javascript - Google 安全浏览 API 中的访问控制允许来源

java - Spring Boot schema.sql - 重启时删除数据库模式

java - this.getClass().getClassLoader().getResourceAsStream 始终返回 null

java - 在登录类的共享首选项中保存字符串以在 MainActivity 中使用 - 无法引用定义的非最终局部变量编辑器

google-api - google drive API 搜索具有给定文件夹 ID 的所有子文件

ios - ios webviews 在打开它们之前检查恶意软件的 url 吗?

java - Google 安全浏览查找 API