JAVA get请求设置请求头
在JAVA中,使用HTTP协议向服务器发送请求是非常常见的操作。有时候我们需要设置请求头,以便在发送请求时传递一些额外的信息给服务器。本文将介绍如何使用JAVA发送GET请求并设置请求头。
1. 使用HttpURLConnection发送GET请求
JAVA提供了java.net.HttpURLConnection
类,可以用来发送HTTP请求。下面是一个发送GET请求并设置请求头的示例代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpGetWithHeadersExample {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 设置请求头
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 发送请求
int responseCode = connection.getResponseCode();
// 读取响应
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 Code: " + responseCode);
System.out.println("Response Message: " + response.toString());
// 关闭连接
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上面的代码中,我们首先创建一个URL
对象,用来表示要发送请求的资源的URL。然后使用url.openConnection()
方法打开连接,返回一个HttpURLConnection
对象。接下来,我们设置请求方法为GET,即connection.setRequestMethod("GET")
。然后,通过connection.setRequestProperty()
方法设置请求头的键值对,例如User-Agent
和Accept-Language
。最后,我们发送请求并读取响应。
2. 使用HttpClient发送GET请求
除了HttpURLConnection,JAVA还提供了Apache HttpClient库,用于发送HTTP请求。使用HttpClient发送GET请求并设置请求头的示例代码如下:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientGetWithHeadersExample {
public static void main(String[] args) {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("
// 设置请求头
request.addHeader("User-Agent", "Mozilla/5.0");
request.addHeader("Accept-Language", "en-US,en;q=0.5");
try {
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response Code: " + statusCode);
System.out.println("Response Body: " + responseBody);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上面的代码中,我们首先创建一个HttpClient
对象,然后创建一个HttpGet
对象,设置请求的URL为"
总结
本文介绍了如何使用JAVA发送GET请求并设置请求头。无论是使用java.net.HttpURLConnection
还是Apache HttpClient库,我们都可以轻松地设置请求头,并向服务器发送请求。希望本文对你有所帮助!
关系图:
erDiagram
USER_AGENT ||--o| HttpURLConnection
ACCEPT_LANGUAGE ||--o| HttpURLConnection
HttpURLConnection }|--o| URL
HttpURLConnection }|..| BufferedReader
HttpURLConnection }|--o| InputStreamReader
HttpURLConnection }o--| API
API }o--| BufferedReader
API }|--| StringBuilder
API }o--| URL
API }|..| InputStreamReader
API }|--| HttpURLConnection
API }|--o| HttpResponse
API }|--| EntityUtils
HttpClient }o--| HttpGet
HttpGet }|--| HttpResponse
HttpGet }|--| EntityUtils
序列图:
sequenceDiagram
participant Client
participant HttpURLConnection
participant URL