Open houdq opened 1 year ago
◎ 互联网上所有资源都要用一个URL来表示 ◎ 必须基于HTTP协议与服务端交互 ◎ 数据展示必须在浏览器中进行
一句话,发起一个HTTP请求的过程就是建立一个Socket通信的过程。java 使用Httpclient 创建链接。
public class HttpClientDemo {
public static void main(String[] args) {
// Create an HttpClient instance
HttpClient httpClient = HttpClients.createDefault();
// Define the URL you want to send the GET request to
String url = "https://baidu.com"; // Replace with your URL
// Create an HttpGet request
HttpGet httpGet = new HttpGet(url);
try {
// Execute the request and get the response
HttpResponse response = httpClient.execute(httpGet);
// Read and display the response
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// Ensure the response entity is fully consumed and the connection is released
response.getEntity().getContent().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
常见的HTTP请求头
请求响应
http 状态码
更多状态码参见 #93
查看 http 信息工具
浏览器缓存机制
浏览器工具查看:Cache-Control:状态若没有缓存则是no-cache
字段 | 描述 | 值 |
---|---|---|
Cache-Control | no-cache | |
Pragma | no-cache | |
Etag: | 是让服务端给每个页面分配一个唯一的编号,然后通过这个编号来区分当前这个页面是否是最新的 | "FkzAS3zbQlpxiu4RLdsxZfuILBYP" |
Last-Modified: | 告诉浏览器这个页面的最后修改时间 | Fri, 25 Aug 2023 04:20:59 GMT |
读书笔记