Java HTTP 设置参数

在Java中,我们经常需要通过HTTP协议与远程服务器进行通信。在使用HTTP协议进行通信时,除了发送请求和接收响应之外,有时我们还需要设置一些参数来满足我们的需求。

本文将介绍如何在Java中使用HTTP协议并设置参数。我们将使用Java的内置类HttpURLConnection来发送HTTP请求,并通过设置不同的参数来实现不同的功能。

发送GET请求

首先,让我们看看如何使用Java发送一个简单的GET请求。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpExample {

    public static void main(String[] args) throws IOException {
        // 创建URL对象
        URL url = new URL("

        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为GET
        connection.setRequestMethod("GET");

        // 获取响应代码
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 读取响应内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 打印响应内容
        System.out.println(response.toString());

        // 关闭连接
        connection.disconnect();
    }
}

上述代码中,我们首先创建一个URL对象,指定要发送请求的目标URL。然后,我们使用openConnection()方法打开与目标URL之间的连接,并将其转换为HttpURLConnection对象。

接下来,我们使用setRequestMethod("GET")方法设置请求方法为GET。这将告诉服务器我们要发送一个GET请求。

然后,我们使用getResponseCode()方法获取响应代码,并将其打印出来。这个响应代码表示服务器是否成功处理了我们的请求。

接下来,我们使用getInputStream()方法获取响应内容的输入流,并用BufferedReader读取并存储响应内容。最后,我们将响应内容打印出来。

最后,我们使用disconnect()方法关闭与服务器的连接。

发送POST请求并设置参数

如果我们需要发送一个带有参数的POST请求,可以使用setRequestMethod("POST")方法,并设置请求的参数。

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class HttpExample {

    public static void main(String[] args) throws IOException {
        // 创建URL对象
        URL url = new URL("

        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        // 设置请求参数
        String parameters = "param1=value1&param2=value2";
        byte[] postData = parameters.getBytes(StandardCharsets.UTF_8);
        int postDataLength = postData.length;
        connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));

        // 发送请求参数
        try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
            wr.write(postData);
        }

        // 获取响应代码
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 读取响应内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 打印响应内容
        System.out.println(response.toString());

        // 关闭连接
        connection.disconnect();
    }
}

上述代码中,我们首先创建一个URL对象和HttpURLConnection对象,然后设置请求方法为POST。

接下来,我们使用setDoOutput(true)方法告诉服务器我们要发送请求参数。

然后,我们将参数转换为字节数组,并计算出参数的长度。我们使用setRequestProperty()方法将参数的长度设置为请求的头部属性。

接下来,我们使用getOutputStream()方法获取输出流,并使用DataOutputStream将参数写入输出流。

最后,我们按照之前的方法获取和处理响应。

其他常见参数设置

除了上述示例中介绍的参数设置之外,我们还可以设置其他常见的参数,如超时时间、