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

StringgetPageContent(String inputUrlString)
get Page Content
URL url = new URL(inputUrlString);
return getPageContent(url);
URLgetRealURL(URL url)
Follow redirections for given url.
URLConnection connection = url.openConnection();
Map<String, List<String>> header = connection.getHeaderFields();
while (isRedirected(header)) {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException();
    url = new URL(header.get("Location").get(0));
    connection = url.openConnection();
...
longgetRemoteTimestamp(final String url)
get Remote Timestamp
try {
    URL u = new URL(url);
    URLConnection urlc = u.openConnection();
    return urlc.getLastModified();
} catch (Exception e) {
    return -1L;
voidgetRequest(URLConnection conn)
get Request
if (useStdHeaders)
    for (int i = 0; i < stdReqHdr.length - 1; i++)
        conn.setRequestProperty(stdReqHdr[i], stdReqHdr[i + 1]);
conn.setDoOutput(true);
conn.connect();
StringgetRequestHeaders(URLConnection conn)
get Request Headers
StringBuffer headers = new StringBuffer();
String key;
int n = 1;
headers.append("\n----------\n");
while ((key = conn.getHeaderFieldKey(n)) != null) {
    String value = conn.getHeaderField(n);
    headers.append(key + ": " + value + "\n");
    n++;
...
StringgetResultsWithEncoding(URLConnection source, String encoding)
Download the URL using specified encoding and return contents as a String.
InputStreamReader in;
if (encoding != null) {
    in = new InputStreamReader(source.getInputStream(), encoding);
} else {
    in = new InputStreamReader(source.getInputStream());
StringBuilder sb = new StringBuilder();
while (true) {
...
ReadergetStream(URL url)
get Stream
URLConnection conn = url.openConnection();
conn.setDefaultUseCaches(false);
conn.setUseCaches(false);
return new InputStreamReader(conn.getInputStream());
URLConnectiongetStreamWithTimeOut(String stringUrl, Integer timeOutInMilli)
get Stream With Time Out
URL url = new URL(stringUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setReadTimeout(timeOutInMilli);
urlConnection.setConnectTimeout(timeOutInMilli);
return urlConnection;
StringgetString(URL url)
get String
URLConnection page = url.openConnection();
page.addRequestProperty("User-Agent", USER_AGENT);
InputStream is = page.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result = "";
String line = "";
while ((line = br.readLine()) != null) {
    result += line + "\r\n";
...
StringgetType(URLConnection connection)
Holt von einer offenen Connection den Mime-Type
return connection.getContentType();