-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
63 lines (59 loc) · 1.98 KB
/
Client.java
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
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String args[]) throws IOException {
Socket socket = new Socket("127.0.0.1", 8080);
Scanner userInput = new Scanner(System.in), socketInput = new Scanner(socket.getInputStream());
PrintStream socketOutput = new PrintStream(socket.getOutputStream());
System.out.println("What function would you like to do?:\n" +
"1- GET\n" +
"2- POST\n" +
"3- DELETE\n");
int choice;
choice = userInput.nextInt();
String URL, Request = "";
if (choice == 1)
{
System.out.print("Enter URL: ");
Request += "GET ";
URL = userInput.next();
Request += URL;
Request += " HTTP/1.1";
Request += "\r\n\r\n";
}
if (choice == 2)
{
System.out.println("Enter URL: ");
Request += "POST ";
URL = userInput.next();
Request += URL;
Request += " HTTP/1.1";
Request += "\r\n";
System.out.println("Enter Body (DONE! to stop): \n");
String line;
while(true)
{
line = userInput.nextLine();
if (line.equals("DONE!"))
break;
Request += line;
Request += "\r\n";
}
Request += "\r\n\r\n";
}
else if (choice == 3)
{
System.out.print("Enter URL: ");
Request += "DELETE ";
URL = userInput.next();
Request += URL;
Request += " HTTP/1.1";
Request += "\r\n\r\n";
}
socketOutput.print(Request);
while(socketInput.hasNextLine())
System.out.println(socketInput.nextLine());
}
}