|
@@ -1,7 +1,38 @@
|
|
1
|
+import java.io.IOException;
|
|
2
|
+import java.net.MalformedURLException;
|
|
3
|
+import java.net.URL;
|
|
4
|
+import java.net.URLConnection;
|
|
5
|
+import java.util.Scanner;
|
|
6
|
+
|
1
|
7
|
public class Main {
|
2
|
8
|
public static void main(String[] args) {
|
3
|
|
- //ask for a url
|
|
9
|
+ System.out.println("Enter a url");
|
|
10
|
+
|
|
11
|
+ // ask for a url
|
|
12
|
+ Scanner scanner = new Scanner(System.in);
|
|
13
|
+ String url = scanner.nextLine().trim();
|
|
14
|
+ System.out.println("Fetching " + url);
|
|
15
|
+
|
4
|
16
|
// fetch the url
|
|
17
|
+ String content = null;
|
|
18
|
+ URLConnection connection = null;
|
|
19
|
+
|
|
20
|
+ try {
|
|
21
|
+ connection = new URL("https://" + url).openConnection();
|
|
22
|
+
|
|
23
|
+ Scanner urlScanner = new Scanner(connection.getInputStream());
|
|
24
|
+ urlScanner.useDelimiter("\\Z"); //terminating line
|
|
25
|
+
|
|
26
|
+ content = urlScanner.next();
|
|
27
|
+
|
|
28
|
+ } catch (MalformedURLException e) {
|
|
29
|
+ e.printStackTrace();
|
|
30
|
+ } catch (IOException e) {
|
|
31
|
+ e.printStackTrace();
|
|
32
|
+ }catch ( Exception ex ) {
|
|
33
|
+ ex.printStackTrace();
|
|
34
|
+ }
|
5
|
35
|
// print the url
|
|
36
|
+ System.out.println(content);
|
6
|
37
|
}
|
7
|
38
|
}
|