|
@@ -0,0 +1,38 @@
|
|
1
|
+package io.zipcode;
|
|
2
|
+
|
|
3
|
+import org.apache.commons.io.IOUtils;
|
|
4
|
+
|
|
5
|
+import java.io.InputStream;
|
|
6
|
+import java.net.URL;
|
|
7
|
+import java.net.URLConnection;
|
|
8
|
+import java.util.Scanner;
|
|
9
|
+
|
|
10
|
+public class URLReader {
|
|
11
|
+
|
|
12
|
+ public static void main(String[] args) {
|
|
13
|
+ Scanner input = new Scanner(System.in);
|
|
14
|
+ System.out.println("Please enter url: ");
|
|
15
|
+ String strUrl = input.nextLine();
|
|
16
|
+
|
|
17
|
+ String body = "";
|
|
18
|
+
|
|
19
|
+ try{
|
|
20
|
+ URL url = new URL(strUrl);
|
|
21
|
+
|
|
22
|
+ URLConnection con = url.openConnection();
|
|
23
|
+ InputStream in = con.getInputStream();
|
|
24
|
+ String encoding = con.getContentEncoding(); // ** WRONG: should use "con.getContentType()" instead but
|
|
25
|
+ // it returns something like "text/html; charset=UTF-8" so this value must be parsed to extract the actual encoding
|
|
26
|
+ encoding = encoding == null ? "UTF-8" : encoding;
|
|
27
|
+ body = IOUtils.toString(in, encoding);
|
|
28
|
+ } catch (Exception e){
|
|
29
|
+
|
|
30
|
+ System.out.println("Exception is " + e );
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ System.out.println(body);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+}
|