Java HTTP Request getContent(String requestUrl)

Here you can find the source of getContent(String requestUrl)

Description

get Content

License

Apache License

Declaration

public static String getContent(String requestUrl) 

Method Source Code

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

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getContent(String requestUrl) {
        InputStream inputStream = null;
        HttpURLConnection httpUrlConn = null;
        BufferedReader bufferedReader = null;
        InputStreamReader inputStreamReader = null;

        try {//from   ww  w.  j  a  va2  s .com
            URL url = new URL(requestUrl);
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            inputStream = httpUrlConn.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            bufferedReader = new BufferedReader(inputStreamReader);

            StringBuffer buffer = new StringBuffer();
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }

            return buffer.toString();

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                bufferedReader.close();
            } catch (Exception e2) {
            }
            try {
                inputStreamReader.close();
            } catch (Exception e2) {
            }
            try {
                inputStream.close();
            } catch (Exception e2) {
            }
            try {
                httpUrlConn.disconnect();
            } catch (Exception e2) {
            }
        }
        return null;
    }
}

Related

  1. addRequestProperties(final Map requestProperties, final HttpURLConnection connection)
  2. doHttpRequest(String action, URL url, String body, String... args)
  3. getDataFromRequestViaPost(String request, String urlParameters)
  4. getOutputFromUrlConnection(String stringUrl, String requestProperty)
  5. getRequest(String urlString)
  6. getRequest(URL url, int timeout)