0

基本上我要做的是使用我创建的 PHP 页面连接到 Pastebin API。好像参数没有输入。这是我的代码:

String urlParameters = "?api_dev_key=" + main.getKey() + "&api_user_name=" + username + "&api_user_password=" + password; URL url = new URL("http://pastebinclient.tk/server/login.php" + urlParameters);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36");

connection.setUseCaches(false);
connection.setDoOutput(true);

我使用具有相同参数的http://hurl.it并且效果很好。在我的页面上,它被设置为如果参数没有全部输入,那么它会返回一条消息说这样,这就是正在发生的事情。抱歉,我问了一个已经回答的问题,但答案没有帮助。

4

2 回答 2

0

您实际上并没有在 POST 请求的正文中提交参数。您将它们作为代码中的查询参数提交。根据 Web 服务的编写方式,它可能接受也可能不接受作为查询参数发送的参数。在这种情况下,它看起来没有。有关如何将参数作为 POST 正文的一部分提交的示例,请参阅Java - 通过 POST 方法轻松发送 HTTP 参数。

于 2017-09-29T04:42:32.923 回答
0

我推荐基于 apache http api的 http-request 。

private static final HttpRequest<?> HTTP_REQUEST = 
      HttpRequestBuilder.createPost("http://pastebinclient.tk/server/login.php")
           .responseDeserializer(ResponseDeserializer.ignorableDeserializer())
           .addDefaultHeader("Content-Type", "application/x-www-form-urlencoded") // I think there is no need
           .addDefaultHeader("Content-Language", "en-US")
           .addDefaultHeader("Accept", "*/*")
           .addDefaultHeader("User-Agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36")
           .build();

public void send(){
   ResponseHandler<?> handler = HTTP_REQUEST.executeWithQuery(urlParameters);
   int statusCode = handler.getStatucCode();
}
于 2017-10-01T21:21:30.203 回答