-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPClient.java
156 lines (129 loc) · 4.26 KB
/
HTTPClient.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.net.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HTTPClient {
public static void main(String[] args) throws Exception {
HTTPMethod method = HTTPMethod.parseMethod(args[0]);
String host = args[1];
int port = 80;
try {
port = Integer.parseInt(args[2]);
} catch (Exception e) {
System.err.println("Argument" + args[2] + " must be an integer.");
System.exit(1);
}
String HTTPversion = args[3];
HTTPClient testClient = new HTTPClient(host, port);
HTTPRequestMessage request = new HTTPRequestMessage(method, "/",
HTTPversion);
testClient.setHTTPRequestMessage(request);
testClient.sendHTTPRequestMessage();
// get embedded objects from the recieved HTTP response message body
String html = testClient.getResponseMessage().getMessageBody();
testClient.getEmbeddedObjects(html);
}
public HTTPClient(String host, int port) {
setHost(host);
setPort(port);
}
public HTTPRequestMessage getRequestMessage() {
return requestMessage;
}
public void setHTTPRequestMessage(HTTPRequestMessage requestMessage) {
this.requestMessage = requestMessage;
requestMessage.setClient(this);
}
private HTTPRequestMessage requestMessage;
public HTTPResponseMessage getResponseMessage() {
return responseMessage;
}
public void setResponseMessage(HTTPResponseMessage responseMessage) {
this.responseMessage = responseMessage;
}
private HTTPResponseMessage responseMessage;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
private String host;
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
private int port;
/**
* Parse the HTTP response message from the server and store as a local
* file.
*/
public void parseHTTPMessage(BufferedReader serverResponse)
throws IOException {
File file = new File("/home/tom/http/index.html");
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
setResponseMessage(new HTTPResponseMessage());
String response;
boolean bodyMessage = false;
boolean headerMessage = true;
System.out.println("================RESPONSE================");
responseMessage.setStatusLine(serverResponse.readLine());
System.out.println(responseMessage.getStatusLine());
while ((response = serverResponse.readLine()) != null) {
System.out.println(response);
if (response.trim().equals("")) {
headerMessage = false;
bodyMessage = true;
continue;
}
if (headerMessage) {
String[] header = response.split(":");
responseMessage.addAsHeader(header[0], header[1]);
} else if (bodyMessage) {
responseMessage.addToMessageBody(response);
bw.write(response + "\n");
}
}
bw.close();
}
public void sendHTTPRequestMessage() throws IOException {
HTTPRequestMessage httpRequest = getRequestMessage();
DataOutputStream outToServer = null;
BufferedReader inFromServer = null;
// Create a socket to the given URI at the given port.
Socket clientSocket = new Socket(host, port);
// Create outputstream (convenient data writer) to this host.
outToServer = new DataOutputStream(clientSocket.getOutputStream());
// Create an inputstream (convenient data reader) to this host.
inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
// Compose HTTP request message and send to the server.
outToServer.writeBytes(httpRequest.composeMessage());
System.out.println("message send: \n" + httpRequest.composeMessage()
+ "to " + host + ":" + port);
// Read HTTP response message from the server, write it to the console
// and write to a local file.
parseHTTPMessage(inFromServer);
if (httpRequest.getHTTPVersion() == "HTTP/1.0") {
clientSocket.close();
}
}
public void getEmbeddedObjects(String html) {
// Use the Jsoup library to parse the html document
Document doc = Jsoup.parse(html);
// select all images from the parsed html document
Elements links = doc.select("img[src]");
for (Element link : links) {
System.out.println("NESTED LINKS: " + link.attr("src"));
}
}
}