forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketGetRequest.java
More file actions
41 lines (29 loc) · 1.06 KB
/
SocketGetRequest.java
File metadata and controls
41 lines (29 loc) · 1.06 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
package com.zetcode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketGetRequest {
public static void main(String[] args) throws IOException {
try (var socket = new Socket("webcode.me", 80)) {
// socket.setSoTimeout(5000);
try (var wtr = new PrintWriter(socket.getOutputStream())) {
// create GET request
wtr.print("GET / HTTP/1.1\r\n");
wtr.print("Host: www.webcode.me\r\n");
wtr.print("\r\n");
wtr.flush();
socket.shutdownOutput();
String outStr;
try (var bufRead = new BufferedReader(new InputStreamReader(
socket.getInputStream()))) {
while ((outStr = bufRead.readLine()) != null) {
System.out.println(outStr);
}
socket.shutdownInput();
}
}
}
}
}