在 Java 中,可以使用`HttpURLConnection`类或者一些流行的 HTTP 客户端库(如`Apache HttpClient`、`OkHttp`等)来发送带有`Header`的`URL`请求。 以下是使用`HttpURLConnection`类的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUrlConnectionWithHeaders { public static void main(String[] args) { String url = "http://example.com"; sendRequestWithHeaders(url); } public static void sendRequestWithHeaders(String urlStr) { try { URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法 connection.setRequestMethod("GET"); // 设置请求头 connection.setRequestProperty("User-Agent", "Mozilla/5.0"); connection.setRequestProperty("Content-Type", "application/json"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine())!= null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } else { System.out.println("请求失败,响应码: " + responseCode); } } catch (IOException e) { e.printStackTrace(); } } } ``` 如果使用`Apache HttpClient`库,示例代码如下: ```java import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class ApacheHttpClientWithHeaders { public static void main(String[] args) { String url = "http://example.com"; sendRequestWithHeaders(url); } public static void sendRequestWithHeaders(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 设置请求头 httpGet.addHeader("User-Agent", "Mozilla/5.0"); httpGet.addHeader("Content-Type", "application/json"); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity); System.out.println(responseString); } catch (IOException e) { e.printStackTrace(); } } } ``` 如果使用`OkHttp`库,示例代码如下: ```java import okhttp3.Headers; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class OkHttpWithHeaders { public static void main(String[] args) { String url = "http://example.com"; sendRequestWithHeaders(url); } public static void sendRequestWithHeaders(String url) { OkHttpClient client = new OkHttpClient(); Headers headers = new Headers.Builder() .add("User-Agent", "Mozilla/5.0") .add("Content-Type", "application/json") .build(); Request request = new Request.Builder() .url(url) .headers(headers) .get() .build(); try (Response response = client.newCall(request).execute()) { System.out.println(response.body().string()); } catch (IOException e) { e.printStackTrace(); } } } ``` [2024-10-29 22:49:54 | AI写代码神器 | 886点数解答]