-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPxReq.java
More file actions
51 lines (41 loc) · 1.56 KB
/
HTTPxReq.java
File metadata and controls
51 lines (41 loc) · 1.56 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
43
44
45
46
47
48
49
50
51
/*import java.io.*;
import java.net.*;
public class HTTPRequest {
public static void main(String[] args) {
try {
URI uri = new URI("http://example.com");
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
System.out.println("Response Code: " + conn.getResponseCode());
System.out.println("Response Message: " + conn.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
System.out.println("\nContent:");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
*/
import java.io.*;
import java.net.*;
public class HTTPxReq {
public static void main(String[] args) throws Exception {
HttpURLConnection c = (HttpURLConnection)
new URL("http://example.com").openConnection();
c.setRequestMethod("GET");
System.out.println("Code: " + c.getResponseCode());
System.out.println("Message: " + c.getResponseMessage());
BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
String line;
while ((line = r.readLine()) != null) System.out.println(line);
r.close();
c.disconnect();
}
}