Java URL Connection doGet(String targetURL)

Here you can find the source of doGet(String targetURL)

Description

Retrieve file contents

License

Apache License

Parameter

Parameter Description
targetURL a parameter
urlParameters a parameter

Return

file contents

Declaration

public static String doGet(String targetURL) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**/*from w  ww.  j  a va2  s  .co m*/
     * Retrieve file contents
     * 
     * @param targetURL
     * @param urlParameters
     * @return file contents
     */
    public static String doGet(String targetURL) {
        URLConnection connection = null;
        try {
            // Create connection
            URL url = new URL(targetURL);
            connection = url.openConnection();

            // Get Response
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            StringBuilder response = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }

            br.close();
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. closeURLClassLoader(URLClassLoader clazzLdr)
  2. createImgUrlConnection(String url)
  3. createURLClassLoader(String dirPath)
  4. createURLConnection(String url)
  5. createWebStartDirectory(String name, String jarUrl)
  6. doGet(String url, String charset)
  7. doPost(final URL url, final Map parameters, final boolean encode)
  8. doPost(String url, String params, String charset)
  9. findClasses(URL resource, String packageName, boolean annotated)