Java Utililty Methods HTTP Read

List of utility methods to do HTTP Read

Description

The list of methods to do HTTP Read are organized into topic(s).

Method

InputStreamgetInputStream(String myUrl)
get Input Stream
InputStream is = null;
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT_MILLIS );
conn.setConnectTimeout(CONNECTION_TIMEOUT_MILLIS );
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
...
InputStreamgetInputStream(String url)
get Input Stream
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(3000);
return con.getInputStream();
InputStreamgetInputStreamFromURL(String urlname)
get Input Stream From URL
InputStream stream = null;
try {
    URL url = new URL(urlname);
    java.net.HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (connection.getResponseCode() != 200 && connection.getResponseCode() != 404
            && connection.getResponseCode() != 202) {
        throw new IOException(connection.getResponseMessage());
    stream = connection.getInputStream();
} catch (MalformedURLException mue) {
    System.err.println("Error: Could create URL object!");
    throw new Exception();
} catch (IOException e) {
    System.err.println("Error: Could not open URL connection!");
    System.err.println(urlname);
    throw new Exception();
return stream;
InputStreamgetInputStreamFromUrl(String urlString)
get Input Stream From Url
URL url = new URL(urlString);
if (!url.getProtocol().toLowerCase().startsWith("http"))
    throw new UnsupportedOperationException(
            "URL \"" + urlString + "\" has an unsupported protocol, \"" + url.getProtocol() + "\".");
else {
    HttpURLConnection urlConn = (HttpURLConnection) new URL(urlString).openConnection();
    urlConn.setConnectTimeout(10000); 
    urlConn.setRequestMethod("GET");
...
InputStreamReadergetInputStreamReader(String httpUrl, int timeout)
Create and open an HTTP connection (GET).
HttpURLConnection connection = createHttpURLConnection(httpUrl, timeout);
if (connection != null) {
    return getInputStreamReader(connection);
return null;
StringgetJsonFromUrlWithJsonParameter(String url, String jsonRequest)
get Json From Url With Json Parameter
URL urll = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urll.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jsonRequest);
out.close();
BufferedReader br;
...
StringgetJsonString(String url)
Get json data from remote server.
String ip = String.valueOf((new Random()).nextInt(254) + 1);
if ((new Random()).nextInt(1) == 1) {
    ip = "220.181.111." + ip;
} else {
    ip = "59.152.193." + ip;
Map<String, String> header = new HashMap<String, String>();
Proxy proxy = null;
...
ReadergetReader(String url)
Obtains a reader for the given URL.
URLConnection connection = new URL(url).openConnection();
if (connection instanceof HttpURLConnection) {
    ((HttpURLConnection) connection).setRequestMethod("GET");
connection.connect();
return new InputStreamReader(connection.getInputStream());
StringgetResult(String urlStr, String content)
get Result
URL url = null;
HttpURLConnection connection = null;
try {
    url = new URL(urlStr);
    connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("POST");
...
byte[]readData(HttpURLConnection conn)
read Data
BufferedInputStream in = null;
try {
    conn.connect();
    int code = conn.getResponseCode();
    in = new BufferedInputStream(conn.getInputStream());
    int len;
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
...