Java HTTP Response getResponseText(URL constructedUrl, String encoding)

Here you can find the source of getResponseText(URL constructedUrl, String encoding)

Description

get Response Text

License

Open Source License

Declaration

public static String getResponseText(URL constructedUrl, String encoding) 

Method Source Code


//package com.java2s;
/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses//from ww w.j a  v a  2 s .  co  m
 */

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

public class Main {
    public static String getResponseText(String url) {
        try {
            return getResponseText(new URL(url), null);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getResponseText(URL constructedUrl, String encoding) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) constructedUrl.openConnection();
            BufferedReader in = null;
            if (null == encoding) {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
            }
            String line;
            final StringBuffer stringBuffer = new StringBuffer(255);

            synchronized (stringBuffer) {
                while ((line = in.readLine()) != null) {
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
                return stringBuffer.toString();
            }
        } catch (final Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
}

Related

  1. getResponseHeaders(URLConnection conn, HashMap headers)
  2. getResponseMessage(InputStream inputStream, HttpURLConnection connection)
  3. getResponseStream(final HttpURLConnection connection)
  4. getResponseStream(HttpURLConnection conn)
  5. getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess)
  6. getResposeText(HttpURLConnection connection)
  7. readResponse(HttpURLConnection conn, String encoding)
  8. readResponse(HttpURLConnection connection)
  9. readResponse(HttpURLConnection httpURLConnection)