-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPconnection.java
More file actions
180 lines (158 loc) · 4.59 KB
/
Copy pathHTTPconnection.java
File metadata and controls
180 lines (158 loc) · 4.59 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package WebCrawler;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.LinkedList;
/**
* The Class HTTPconnection.
*/
public class HTTPconnection
{
/** The DEFAULT protocol used */
private String PROTOCOL = "HTTP/1.1";
/** The DEFAULT connection. */
private String CONNECTION = "close";
/** The DEFAULT cache control. */
private String CACHE_CONTROL = "max-age=0";
/** The DEFAULT accept. */
private String ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
/** The DEFAULT content type. */
private String CONTENT_TYPE = "application/x-www-form-urlencoded";
/** The DEFAULT user agent. */
private String USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36";
/** The DEFAULT accept lunguage. */
private String ACCEPT_LUNGUAGE = "en-us";
/** The current url fetched. */
private Url currentURL;
/** The linked list of request header. */
private LinkedList<String> requestHearder;
/** The linked list of posted message. */
private LinkedList<String> postMessage;
/** The Symbol of new line. */
private String NEW_LINE = "\n";
/** The socket client. */
private Socket client = null;
/** The sock addr. */
private InetSocketAddress sockAddr = null;
/**
* Instantiates a new HTTP connection.
* Create URL, requestHeader, and message body
* @param URL the url
*/
public HTTPconnection(Url URL)
{
this.currentURL = URL;
requestHearder = new LinkedList<String>();
postMessage = new LinkedList<String>();
}
/**
* Sets the request.
* Add new request in request header
* @param request the new request
*/
public void setRequest(String request)
{
String httpCommand = request + " " + currentURL.getPath() + " " + PROTOCOL + NEW_LINE;
requestHearder.add(httpCommand);
}
/**
* Sets the header.
* Add new header in header list
* @param headerName the header name
* @param headerValue the header value
*/
public void setHeader(String headerName, String headerValue)
{
String header = headerName + ": " + headerValue + NEW_LINE;
requestHearder.add(header);
}
/**
* Sets the default header.
*/
public void setDefaultHeader()
{
requestHearder.add("Connection: " + CONNECTION + NEW_LINE);
requestHearder.add("Cache-Control: " + CACHE_CONTROL + NEW_LINE);
requestHearder.add("ACCEPT: " + ACCEPT + NEW_LINE);
requestHearder.add("CONTENT_TYPE: " + CONTENT_TYPE + NEW_LINE);
requestHearder.add("USER_AGENT: " + USER_AGENT + NEW_LINE);
requestHearder.add("ACCEPT_LUNGUAGE: " + ACCEPT_LUNGUAGE + NEW_LINE);
}
/**
* Sets the post content.
* Add new content POST message body
* @param sentContent the new post content
*/
public void setPostContent(String sentContent)
{
postMessage.add(sentContent);
}
/**
* Sets the cookies.
* Add more cookies in cookie list
* @param cookies the new cookies
*/
public void setCookies(LinkedList<Cookie> cookies)
{
if (cookies != null)
{
String sentCookie = cookies.get(0).getName() + "=" + cookies.get(0).getValue();
for (int i = 1; i < cookies.size(); i++)
{
sentCookie = sentCookie + "; " + cookies.get(i).getName() + "="
+ cookies.get(i).getValue();
}
sentCookie = "Cookie: " + sentCookie + NEW_LINE;
requestHearder.add(sentCookie);
// System.out.println(sentCookie);
}
}
/**
* Process url.
* Processing URL according to the set of request, request header
* and message body (if there is any)
* @return the page
*/
public Page processURL()
{
String content = "";
try
{
client = new Socket();
sockAddr = new InetSocketAddress(currentURL.getHost(), currentURL.getPort());
client.connect(sockAddr, 1000);
Writer writer = new OutputStreamWriter(client.getOutputStream());
while (!requestHearder.isEmpty())
{
String request = requestHearder.remove();
writer.write(request);
}
writer.write(NEW_LINE);
while (!postMessage.isEmpty())
{
String message = postMessage.remove();
writer.write(message);
}
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String message = "";
while ((message = reader.readLine()) != null)
{
content += message + NEW_LINE;
}
writer.close();
client.close();
}
catch (Exception e)
{
System.out.println("Cannot get connect to address: " + sockAddr.getHostName());
e.printStackTrace();
}
Page currentPage = new Page(content);
return currentPage;
}
}