-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPResponse.java
More file actions
32 lines (27 loc) · 1.13 KB
/
Copy pathHTTPResponse.java
File metadata and controls
32 lines (27 loc) · 1.13 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
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
public class HTTPResponse {
private OutputStream outputStream;
public HTTPResponse(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void sendResponse(int statusCode, String statusMessage, String contentType, byte[] content)
throws IOException {
String response = "HTTP/1.0 " + statusCode + " " + statusMessage + "\r\n" +
"Date: " + new Date() + "\r\n" +
"Server: HTTPServer\r\n" +
"Content-Length: " + content.length + "\r\n" +
"Content-Type: " + contentType + "\r\n\r\n";
outputStream.write(response.getBytes());
outputStream.write(content);
outputStream.flush();
}
public void sendError(int errorCode, String errorMessage) throws IOException {
String response = "HTTP/1.0 " + errorCode + " " + errorMessage + "\r\n" +
"Date: " + new Date() + "\r\n" +
"Server: HTTPServer\r\n\r\n";
outputStream.write(response.getBytes());
outputStream.flush();
}
}