Java Utililty Methods URL Connection

List of utility methods to do URL Connection

Description

The list of methods to do URL Connection are organized into topic(s).

Method

StringgetUrl(String serverName, String docid, String pageid)
get Url
String url = "http://" + serverName + "/pages/uri_template";
String template = getFromUrl(url);
if (template.contains("{pageid}"))
    template = template.replace("{pageid}", pageid);
if (template.contains("{docid}"))
    template = template.replace("{docid}", docid);
return template;
StringgetURL(String url)
get URL
URLConnection conn = new URL(url).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String result = in.readLine();
return result;
PropertiesgetUrlAsProperties(String urlString)
get Url As Properties
Properties props = new Properties();
InputStream in = getUrlAsStream(urlString);
props.load(in);
in.close();
return props;
StringgetUrlContent(String urlAddress)
get Url Content
URL url = new URL(urlAddress);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
InputStream inStream = null;
inStream = connection.getInputStream();
InputStreamReader inStreamReader = new InputStreamReader(inStream);
BufferedReader buffer = new BufferedReader(inStreamReader);
StringBuffer strbuf = new StringBuffer();
...
StringgetURLContents(URL u, Map data)
get URL Contents
URLConnection conn = u.openConnection();
conn.setDoOutput(true);
OutputStreamWriter postWriter = new OutputStreamWriter(conn.getOutputStream());
boolean first = true;
for (Entry<String, String> ent : data.entrySet()) {
    if (!first)
        postWriter.write("&");
    String set = URLEncoder.encode(ent.getKey(), "UTF-8") + "="
...
StringgetURLContentsAsString(URL url)
Get the contents of a URL as a string
StringBuffer ret = new StringBuffer();
BufferedReader br = null;
try {
    URLConnection conn = url.openConnection();
    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        ret.append(line).append("\n");
...
ListgetURLDataList(URL StrurlStringing)
get URL Data List
List list = new ArrayList();
try {
    System.out.println("Loading - " + StrurlStringing);
    URLConnection connection = StrurlStringing.openConnection();
    connection.connect();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String str = in.readLine();
    while (str != null) {
...
StringgetUrlEncoding(URLConnection connection)
get Url Encoding
String contentType = connection.getContentType();
String[] values = contentType.split(";");
String charset = defaultEncoding; 
for (String value : values) {
    value = value.trim();
    if (value.toLowerCase(Locale.ENGLISH).startsWith("charset=")) {
        charset = value.substring("charset=".length());
return charset;
URLgetURLForward(URL url)
get URL Forward
URLConnection con = url.openConnection();
String location = con.getHeaderField("Location");
if (location != null) {
    return new URL(location);
return url;
InputStreamgetURLInputStream(final URL url)
Get an InputStream from a URL.
return getURLInputStream(url, DEFAULT_URL_CONNECT_TIMEOUT, DEFAULT_URL_READ_TIMEOUT);