-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlConnection.java
33 lines (29 loc) · 1.05 KB
/
UrlConnection.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
// 13. Write a program for Demostrate URLConnection.
import java.io.*;
import java.net.*;
import java.util.*;
public class UrlConnection {
public static void main(String[] args) {
System.out.println("Enter URL:");
Scanner input = new Scanner(System.in);
String myurl = input.nextLine();
try {
// Open the URLConnection for reading
URL u = new URL(myurl);
URLConnection uc = u.openConnection();
try (InputStream raw = uc.getInputStream()) { // autoclose
InputStream buffer = new BufferedInputStream(raw);
// chain the InputStream to a Reader
Reader reader = new InputStreamReader(buffer);
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
}
} catch (MalformedURLException ex) {
System.err.println(myurl + " is not a parseable URL");
} catch (IOException ex) {
System.err.println(ex);
}
}
}