Java HTTP Get executeGet(String targetURL)

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

Description

execute Get

License

Open Source License

Declaration

public static String executeGet(String targetURL) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String executeGet(String targetURL) {
        HttpURLConnection connection = null;
        try {//from w  w w .ja  v a2  s  . c o  m
            //Create connection
            URL url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoOutput(true);

            //Get Response  
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            StringBuilder response = new StringBuilder(); // or StringBuffer if not Java 5+ 
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();
        } catch (Exception e) {
            return e.getMessage();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}

Related

  1. doGet(String url, HashMap params)
  2. doGet(String urlStr)
  3. doGet(String urlString)
  4. doGet(URL url, String... args)
  5. doGetRequest(String urlStr)
  6. getHttpURLConnectionByGET(String resourceURL)
  7. getJsonFromUrl(String url)
  8. httpGet(String httpUrl)
  9. httpGet(String url)