-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHttpClient.java
132 lines (121 loc) · 3.63 KB
/
MyHttpClient.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import request.Util;
import request.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* this class provides methods to communicate with a server
*/
public class MyHttpClient {
private final int PORT;
private final String HOST_NAME;
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private Logger logger;
/**
* Creates a MyHttpClient with of a server with a given hostname and connects trough a given portNumber
* @param hostName server hostname
* @param portNumber server port to make connection
* @throws IOException
*/
public MyHttpClient(String hostName, int portNumber) throws IOException {
this.HOST_NAME = hostName;
this.PORT = portNumber;
this.socket = new Socket(this.HOST_NAME,this.PORT);
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream(),true);
this.logger = new Logger();
}
/**
* Sends a http GET request
* @param ObjectName name of the file to access
* @throws IOException
*/
public void getResource(String ObjectName) throws IOException {
this.logger.info("Making GET request to server");
out.print(
String.format(
"GET /%s HTTP/1.1%sHost: %s%s",
ObjectName, Util.CRLF, this.HOST_NAME, Util.CRLF
)
);
out.flush();
this.logger.answer(in);
}
/**
* Sends a http POST request
* @param data data to send to the server
* @throws IOException
*/
public void postData(String[] data) throws IOException {
this.logger.info("Making POST request to server");
out.print(
String.format(
"POST /simpleForm.html HTTP/1.1%sHost: %s%s%s%s",
Util.CRLF, this.HOST_NAME, Util.CRLF, Util.CRLF, String.format("{%s,%s}", data[0], data[1])
)
);
out.flush();
this.logger.answer(in);
}
/**
* Sends an http request with a method name not supported by MyHttpServer
* @param wrongMethodName the unimplemented method to send
* @throws IOException
*/
public void sendUnimplementedMethod (String wrongMethodName) throws IOException {
this.logger.info("Making a request to an unimplementedMethod to server");
out.print(
String.format(
"%s /index.html HTTP/1.1%s",
wrongMethodName, Util.CRLF
)
);
out.flush();
this.logger.answer(in);
}
/**
* Sends a malformed http GET of types i -> character "\r\n" not present after the request line,
* ii -> presence of additional space characters between the fields of the request line
* iii -> HTTP version field not present in the request
* @param type the type of malformedRequest to send
* @throws IOException
*/
public void malformedRequest(int type) throws IOException {
switch (type) {
case 1:
this.logger.info("Making a malformed request of the i type to server");
out.print("GET /index.html HTTP/1.1");
break;
case 2:
this.logger.info("Making a malformed request of the ii type to server");
out.print("GET /index.html HTTP/1.1 " + Util.CRLF);
break;
case 3:
this.logger.info("Making a malformed request of the iii type to server");
out.print("GET /index.html" + Util.CRLF);
break;
}
out.flush();
this.logger.answer(in);
}
/**
* closes the communication between client and server
*/
public void close() {
try {
out.print("DELETE /session HTTP/1.1" + Util.CRLF + "Connection: close" + Util.CRLF);
out.flush();
this.in.close();
this.out.close();
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}