HttpURL

Time: 2024-04-10 16:32:26
Author: Jackasher

HttpURL

模拟浏览器发生请求,User-Agent,Method和URL是必须的,接下来模拟浏览器发生请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//创建HttpUrl
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();

//设置Method和User-Agent属性
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36","true");

//获取读取的流
InputStream inputStream = httpURLConnection.getInputStream();

//包装一下便于读取
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

//使用readline读取
String msg = null;
while ((msg=bufferedReader.readLine()) != null ){
System.out.println(msg);
}

HttpURL
http://example.com/2024/04/10/HttpURL/
作者
Jack Asher
发布于
2024年4月10日
许可协议