Java HTTP Get doGetRequest(String urlStr)

Here you can find the source of doGetRequest(String urlStr)

Description

do Get Request

License

LGPL

Declaration

public static String doGetRequest(String urlStr) 

Method Source Code

//package com.java2s;
/**/*from w  ww .j  a va  2 s .  c  om*/
 * Utilities about the http protocol, currently featuring get requests
 *
 * License: LGPLv3
 *
 * @author Janmm14
 * @since 2.0.0
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String doGetRequest(String urlStr) {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related

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