Java HTTP Get httpGet(String httpUrl)

Here you can find the source of httpGet(String httpUrl)

Description

http Get

License

Apache License

Declaration

public static String httpGet(String httpUrl) 

Method Source Code


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

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

public class Main {
    public static String httpGet(String httpUrl) {
        BufferedReader input = null;
        StringBuilder sb = null;/* www  . j  a v a 2s. c o  m*/
        URL url = null;
        HttpURLConnection con = null;
        try {
            url = new URL(httpUrl);
            try {
                con = (HttpURLConnection) url.openConnection();
                input = new BufferedReader(new InputStreamReader(con.getInputStream()));
                sb = new StringBuilder();
                String s;
                while ((s = input.readLine()) != null) {
                    sb.append(s).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } finally {
            // close buffered
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // disconnecting releases the resources held by a connection so they may be closed or reused
            if (con != null) {
                con.disconnect();
            }
        }

        return sb == null ? null : sb.toString();
    }
}

Related

  1. doGet(URL url, String... args)
  2. doGetRequest(String urlStr)
  3. executeGet(String targetURL)
  4. getHttpURLConnectionByGET(String resourceURL)
  5. getJsonFromUrl(String url)
  6. httpGet(String url)
  7. httpGet(String url, boolean logStdout)
  8. httpGet(String url, StringBuffer response)
  9. httpGet(String urlStr)