This repository was archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebPage.java
More file actions
93 lines (71 loc) · 2.93 KB
/
Copy pathWebPage.java
File metadata and controls
93 lines (71 loc) · 2.93 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
package utils;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import static utils.Constants.*;
public class WebPage {
private String content;
private String url;
private WebPage(String url, String content) {
this.url = url;
this.content = content;
}
public String getContent() {
return content;
}
public String getUrl() {
return url;
}
public static WebPage loadWebPage(String address, String encoding) throws IOException {
return loadWebPage(address, encoding, null);
}
public static WebPage loadWebPage(String address, String encoding, String postData) throws IOException {
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1");
if (postData != null)
writePostData(postData, conn);
encoding = findRealEncoding(encoding, conn);
String response = readResponse(conn, encoding);
return new WebPage(address, response);
}
private static String findRealEncoding(String presetEncoding, URLConnection connection) {
String realEncoding = connection.getContentEncoding();
if (realEncoding != null)
return realEncoding;
String type = connection.getContentType();
if (type != null) {
final String CHARSET_ATTRIBUTE = "charset=";
int pos = type.indexOf(CHARSET_ATTRIBUTE);
if (pos != NOT_FOUND)
return type.substring(pos + CHARSET_ATTRIBUTE.length() );
}
return presetEncoding;
}
private static void writePostData(String postData, URLConnection connection) throws IOException {
connection.setDoOutput(true);
OutputStreamWriter writter = new OutputStreamWriter( connection.getOutputStream() );
writter.write(postData);
writter.flush();
writter.close();
}
private static String readResponse(URLConnection connection, String encoding) throws IOException {
if ( !isValidContentType(connection) )
throw new IOException("Invalid content type for " + connection.getURL());
BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream(), encoding) );
String line, lines = "";
while (( line = reader.readLine() ) != null)
lines += line + "\n";
reader.close();
return lines;
}
private static boolean isValidContentType(URLConnection connection) {
String type = connection.getContentType();
boolean isValid = true;
if (type != null && type.length() >= 5) {
String category = type.substring(0, 5);
isValid = !category.equals("image") && !category.equals("audio") && !category.equals("video");
}
return isValid;
}
}