Java 实现浏览器可访问的带 Header 的 URL 请求:HttpURLConnection、Apache HttpClient 与 OkHttp 示例
在 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(); } } } ``` ######[AI写代码神器 | 886点数解答 | 2024-10-29 22:49:54]
- Python实现深度优先爬虫:精准爬取百度域名下链接,模拟输出详细信息(GPT | 2142点数解答 | 2025-05-13 15:54:49)311
- Java代码:如何通过 POST 请求向指定 URL 保存报告( | 19点数解答 | 2024-05-20 09:08:06)330
- Java 代码纠错:搞定向指定 URL 发送 POST 请求的语法问题( | 404点数解答 | 2024-05-20 09:08:20)342
- 解决Java发送POST请求接收中文乱码问题:代码实现与优化建议( | 1689点数解答 | 2024-05-20 09:09:07)355
- 易语言实现高并发 HTTP 操作:多线程、线程池与异步操作实战(字节豆包 | 849点数解答 | 2025-06-03 23:17:33)297
- App.vue 中头部代码结构与样式剖析(字节豆包 | 31点数解答 | 2025-05-14 09:58:18)266
- Java 实现复制粘贴功能:打造简易文本复制粘贴程序(GPT | 1497点数解答 | 2024-08-25 09:40:33)438
- PHP代码:循环检测域名状态,借助HTTP请求与CURL实现(GPT | 947点数解答 | 2024-10-27 11:56:50)277
- Maven POM文件开头元素详解及Spring Boot项目配置规范(DeepSeek | 1912点数解答 | 2026-04-23 16:43:34)20
- 解决 Java 中 HTTP 请求方法名含非法字符异常:示例与注意要点(阿里通义 | 321点数解答 | 2024-12-03 17:34:09)378
- Java实现链表反转:迭代与递归双解法详解及开发实战指南(DeepSeek | 1409点数解答 | 2026-03-15 15:09:29)62
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)337