core.RESTCalls.RESTGet.java Source code

Java tutorial

Introduction

Here is the source code for core.RESTCalls.RESTGet.java

Source

/**
 * Copyright 2014 SeaClouds
 * Contact: Dionysis Athanasopoulos <dionysiscsuoi@gmail.com>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package core.RESTCalls;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class RESTGet {

    public static String httpGet(String urlStr) throws Exception {

        HttpClient client = new DefaultHttpClient();

        HttpGet get = new HttpGet(urlStr);

        HttpResponse response = client.execute(get);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String content = "", line = null;

        while ((line = rd.readLine()) != null)
            content += line + "\n";

        return content;
    }

    public static InputStream httpGetResponse(String urlStr) {

        InputStream inputStream = null;

        try {

            HttpClient client = new DefaultHttpClient();

            HttpGet get = new HttpGet(urlStr);

            HttpResponse response = client.execute(get);

            inputStream = response.getEntity().getContent();
        }

        catch (Exception ex) {

            ex.printStackTrace();
        }

        return inputStream;
    }
}