Example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient.

Prototype

public DefaultHttpClient() 

Source Link

Usage

From source file:Main.java

public static InputStream loadFileFromURL(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;//from ww w .  j  a v  a 2s.c o  m
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return httpEntity.getContent();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static InputStream retrieveStream(String url) {

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);

    try {//from w w  w  . j av a  2  s. c o  m

        HttpResponse getResponse = client.execute(getRequest);
        final int statusCode = getResponse.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();

    } catch (IOException e) {
        getRequest.abort();
    }

    return null;

}

From source file:Main.java

public static byte[] loadByteFromURL(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;//  w ww  .  j a v  a2  s . c om
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return EntityUtils.toByteArray(httpEntity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String loadTextFromURL(String url, String charset) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;//  w w  w  . j a  va 2  s  . c om
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return EntityUtils.toString(httpEntity, charset);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String makeGETRequest(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);

    // Fazendo a requisicao ao servidor
    try {/*from ww  w .j  a  va2 s .  c  o m*/
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return httpclient.execute(get, responseHandler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalStateException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static HttpResponse postJson(String path, ArrayList<NameValuePair> params) throws Exception {

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(path);
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    httpPost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    httpPost.setHeader("X-Requested-With", "XMLHttpRequest");

    return httpClient.execute(httpPost);
}

From source file:Main.java

public static String getJSONFromUrl(String url) {

    try {/*from w  ww . j  a va 2s .  com*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        //            httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:getopendata.HttpUtils.java

public static HttpResponse httpGet(String url) throws IOException {

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpget);

    return response;
}

From source file:Main.java

public static String getStringFromUrl(String url) throws ClientProtocolException, IOException {
    HttpGet get = new HttpGet(url);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity, "UTF-8");
}

From source file:com.markwatson.linkeddata.DBpediaLookupClientJson.java

public static String lookup(String query) {
    StringBuffer sb = new StringBuffer();
    try {/*ww w. j a v a  2  s.c o m*/
        HttpClient httpClient = new DefaultHttpClient();
        String query2 = query.replaceAll(" ", "+");
        HttpGet getRequest = new HttpGet(
                "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
        getRequest.addHeader("accept", "application/json");
        HttpResponse response = httpClient.execute(getRequest);
        if (response.getStatusLine().getStatusCode() != 200)
            return "Server error";

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        httpClient.getConnectionManager().shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}