📜  在线 java http 请求演示 - Java (1)

📅  最后修改于: 2023-12-03 15:37:45.432000             🧑  作者: Mango

在线 Java HTTP 请求演示

本演示将向您介绍如何使用 Java 进行 HTTP 请求,并演示一些常见的 HTTP 请求方式和参数。

1. 发起 GET 请求
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

System.out.println(response.toString());

以上代码演示了如何向 https://jsonplaceholder.typicode.com/posts/1 发送一个 GET 请求,并输出该请求的响应内容。

2. 发起 POST 请求
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");

String postJsonData = "{\n"
    + "  \"title\": \"foo\",\n"
    + "  \"body\": \"bar\",\n"
    + "  \"userId\": 1\n"
    + "}";

con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(postJsonData);
out.flush();
out.close();

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

System.out.println(response.toString());

以上代码演示了如何向 https://jsonplaceholder.typicode.com/posts 发送一个 POST 请求,并输出该请求的响应内容。

3. 发起 PUT 请求
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");

String putJsonData = "{\n"
    + "  \"id\": 1,\n"
    + "  \"title\": \"foo\",\n"
    + "  \"body\": \"bar\",\n"
    + "  \"userId\": 1\n"
    + "}";

con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(putJsonData);
out.flush();
out.close();

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

System.out.println(response.toString());

以上代码演示了如何向 https://jsonplaceholder.typicode.com/posts/1 发送一个 PUT 请求,并输出该请求的响应内容。

4. 发起 DELETE 请求
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE");

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);

以上代码演示了如何向 https://jsonplaceholder.typicode.com/posts/1 发送一个 DELETE 请求。

5. 总结

本演示向您介绍了如何使用 Java 进行常见的 HTTP 请求,并演示了 GET、POST、PUT 和 DELETE 请求的发送方法和响应结果输出。在实际开发中,您可以根据业务需求和 API 文档来选择合适的请求方式和参数,以实现您的业务逻辑。