-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetContentType.java
38 lines (36 loc) · 1.37 KB
/
GetContentType.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
//Download a web page with the correct character set
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class GetContentType {
public static void main(String[] args) {
System.out.println("Enter URL:");
Scanner input = new Scanner(System.in);
String myurl = input.nextLine();
try {
// set default encoding
String encoding = "ISO-8859-1";
URL u = new URL(myurl);
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int encodingStart = contentType.indexOf("charset=");
if (encodingStart != -1) {
encoding = contentType.substring(encodingStart + 8);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in, encoding);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
r.close();
} catch (MalformedURLException ex) {
System.err.println(myurl + " is not a parseable URL");
} catch (UnsupportedEncodingException ex) {
System.err.println(
"Server sent an encoding Java does not support: " + ex.getMessage());
} catch (IOException ex) {
System.err.println(ex);
}
}
}