我正在开发一个程序,该程序会查询 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 查找错误的说明:
服务器为 POST 请求生成以下 HTTP 错误代码:
•200:在网络钓鱼、恶意软件或不需要的软件列表中,至少有一个查询的 URL 匹配。实际结果通过响应正文返回。
•204:查询的网址中没有一个与钓鱼、恶意软件或垃圾软件列表匹配,并且没有返回响应正文。
•400:错误请求—HTTP 请求的格式不正确。
•401:未授权—API 密钥未授权。
•503:服务不可用——服务器无法处理请求。除了正常的服务器故障外,这也可能表明客户端已因发送过多请求而受到“限制”。
响应代码 403 未列出,但我知道了。
我已经对我的 API 密钥进行了三次检查,并确保为我的项目启用了 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();
}