-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApacheTest.java
More file actions
42 lines (35 loc) · 1.38 KB
/
ApacheTest.java
File metadata and controls
42 lines (35 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.dj;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ApacheTest {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://example.org");
request.addHeader( "User-Agent","Chrome");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(request);
System.out.println("Response code = " + response.getReasonPhrase());
BufferedReader inputReader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = inputReader.readLine()) != null) {
System.out.println(line);
}
inputReader.close();
} catch (IOException e) {
System.out.println("IOException = " + e.getMessage());
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}