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

ReadergetFeedReader(URL feedUrl)
Creates a reader with the appropriate char encoding for the URL
Reader reader;
URLConnection conn = feedUrl.openConnection();
if (feedUrl.getProtocol().equals("http") || feedUrl.getProtocol().equals("https")) {
    String contentTypeHeader = conn.getContentType();
    String encoding = "ISO-8859-1";
    if (contentTypeHeader != null) {
        Matcher matcher = CHARSET_PATTERN.matcher(contentTypeHeader);
        if (matcher.find()) {
...
StringgetFromUrl(String url)
get From Url
URL loc = new URL(url);
URLConnection pagesService = loc.openConnection();
InputStream is = pagesService.getInputStream();
StringBuilder sb = new StringBuilder();
while (is.available() != 0) {
    byte[] data = new byte[is.available()];
    is.read(data);
    sb.append(new String(data));
...
Inet4AddressgetGlobalAddress(String url)
Finds this computer's global IP address
try {
    URLConnection uc = new URL(url).openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    return (Inet4Address) InetAddress.getByName(br.readLine());
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
...
StringgetGlobalIPAddress(URL automationPage)
Returns the global (not the local) IP address of the host by using the given automation page.
InputStream inputStream = null;
try {
    URLConnection urlConnection = automationPage.openConnection();
    int contentLength = Integer.valueOf(urlConnection.getHeaderField("Content-Length"));
    byte[] buffer = new byte[contentLength];
    inputStream = urlConnection.getInputStream();
    inputStream.read(buffer);
    return new String(buffer);
...
longgetHeaderFieldLong(URLConnection conn, String name, long Default)
Java 7 method
String value = conn.getHeaderField(name);
try {
    return Long.parseLong(value);
} catch (Exception e) {
return Default;
StringgetHTML(String url, boolean removeNonLatinChars)
get HTML
String line = "", all = "";
URL myUrl = null;
BufferedReader br = null;
try {
    myUrl = new URL(url);
    URLConnection con = myUrl.openConnection();
    con.setConnectTimeout(1000 * 8);
    con.setReadTimeout(1000 * 8);
...
StringgetHTML(URL url)
get HTML
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent",
        "Mozilla/5.0 " + "(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                + "(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
...
StringgetHttpGetContent(String strUrl, String charSet)
get Http Get Content
URL url = new URL(strUrl);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charSet));
StringBuffer sb = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
    sb.append(inputLine).append(System.lineSeparator());
return sb.toString();
MapgetHttpHeaders(URLConnection connection)
Extract HTTP headers from a connection.
Map<String, String> mapHeaders = new HashMap<>();
for (Map.Entry<String, List<String>> entries : connection.getHeaderFields().entrySet()) {
    mapHeaders.put(entries.getKey() == null ? "Status code" : entries.getKey(),
            String.join(",", entries.getValue()));
return mapHeaders;
MapgetHttpResponseHeader(URLConnection http)
get Http Response Header
Map<String, String> header = new LinkedHashMap<String, String>();
for (int i = 0;; i++) {
    String mine = http.getHeaderField(i);
    if (mine == null)
        break;
    String str = http.getHeaderFieldKey(i);
    if (str != null) {
        header.put(str.toLowerCase(Locale.CHINA), mine);
...