1

我在编程时遇到了一些困难,请求正文的含义很混乱。它总是返回 400 个响应代码。请帮助我。

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

1 回答 1

2

你的要求是错误的。如果您使用 POST 请求,则参数clientapikeyapiverpver应该是 URL 的一部分。

请求正文应仅包含要检查的 URL(加上第一行的 URL 数量)。

所以它可能看起来像这样:

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();
于 2012-12-27T13:57:51.953 回答