-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlConnectionReader.java
33 lines (29 loc) · 1.23 KB
/
UrlConnectionReader.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
//package com.itersdesktop.javatechs;
import java.net.*;
import java.io.*;
public class UrlConnectionReader {
public static void main(String[] args) {
String theUrl = "https://www.ebi.ac.uk/biomodels/model/download/BIOMD0000000758.3?filename=Babbs2012.xml";
String output = getUrlContents(theUrl);
System.out.println(output);
}
private static String getUrlContents(String theUrl) {
StringBuilder content = new StringBuilder();
// Use try and catch to avoid the exceptions
try {
URL url = new URL(theUrl); // creating a url object
URLConnection urlConnection = url.openConnection(); // creating a urlconnection object
// wrapping the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// reading from the urlconnection using the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}