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

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

Introduction

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

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:eu.scape_project.fcrepo.integration.AbstractIT.java

@BeforeClass
public static void init() throws Exception {
    /* wait for an existing ContainerWrapper to finish */
    try {/*ww w .  java 2 s . c o m*/
        HttpGet get = new HttpGet("http://localhost:8080");
        DefaultHttpClient client = new DefaultHttpClient();
        while (client.execute(get).getStatusLine().getStatusCode() > 0) {
            Thread.sleep(1000);
            System.out.println("Waiting for existing application server to stop...");
        }
    } catch (ConnectException e) {
        // it's good, we can't connect to 8080
        // don't do exec flow by exception handling though ;)
    }
}

From source file:com.bigdata.rockstor.console.RockStorSender.java

public static HttpResp perform(HttpReq req) throws URISyntaxException, ClientProtocolException, IOException {

    HttpResp resp = null;/*from  w  ww  . j  a v  a2 s.co m*/
    HttpRequestBase request = buildHttpRequest(req);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);
    resp = buildHttpResponse(response);

    return resp;
}

From source file:com.skalski.raspberrycontrol.Mjpeg.Mjpeg_InputStream.java

public static Mjpeg_InputStream read(String url) throws IOException {
    HttpResponse res;/*from  w  ww.j a  v a2s .  c o  m*/
    DefaultHttpClient httpclient = new DefaultHttpClient();

    res = httpclient.execute(new HttpGet(URI.create(url)));
    return new Mjpeg_InputStream(res.getEntity().getContent());
}

From source file:de.randec.MVBMonitor.Downloader.java

static String downloadJson(String url) throws IOException {
    String data = "";
    try {/*  www.jav  a  2 s. c o m*/
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost);
        data = EntityUtils.toString(response.getEntity(), "utf-8");
        //data = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    //unescape escape codes (Umlaute)
    return StringEscapeUtils.unescapeHtml4(data);

}

From source file:com.starbucks.apps.HttpUtils.java

private static HttpInvocationContext invoke(HttpUriRequest request, final String payload,
        final String contentType) throws IOException {

    if (payload != null) {
        HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
        EntityTemplate ent = new EntityTemplate(new ContentProducer() {
            public void writeTo(OutputStream outputStream) throws IOException {
                outputStream.write(payload.getBytes());
                outputStream.flush();// ww w. j  a va 2  s  .  com
            }
        });
        ent.setContentType(contentType);
        entityEncReq.setEntity(ent);
    }

    HttpInvocationContext context = new HttpInvocationContext(payload);
    DefaultHttpClient client = getHttpClient(context);
    HttpResponse response = client.execute(request);
    context.setHttpResponse(response);
    return context;
}

From source file:Main.java

private static InputStream retrieveStream(String url) {

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);

    try {/*from   ww  w  .jav a  2 s . com*/

        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:com.mehmetakiftutuncu.eshotroid.utilities.Request.java

public static String get(String url) {
    try {//from   w w w . java 2 s. c o m
        Log.info(Request.class, "Making GET request to url: " + url);

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            Log.error(Request.class,
                    "GET request failed, invalid status code! status code: " + statusCode + ", url: " + url);
            return null;
        } else {
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");

            Log.info(Request.class, "GET result from url: " + url);
            Log.info(Request.class, result);

            return result;
        }
    } catch (Exception e) {
        Log.error(Request.class, "GET request failed! url: " + url, e);
        return null;
    }
}

From source file:com.hagreve.android.lib.HaGreveApi.java

/**
 * Performs an HTTP GET request to the specified URL resource.
 * @param url/*from  www.  j a  v a 2s .c o m*/
 * @return Content of the resource, as a String.
 */
private static String doGetRequest(String url) {
    String ret = null;
    HttpResponse response = null;
    HttpGet request = new HttpGet(url);
    request.setHeader("User-Agent", USER_AGENT);
    request.setHeader("Accept", "application/json");
    try {
        DefaultHttpClient client = new DefaultHttpClient();
        response = client.execute(request);
    } catch (IOException e) {
        Log.d(API_LOG_TAG, "Error getting " + url);
    }

    if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        try {
            InputStream in = new BufferedInputStream(response.getEntity().getContent());
            ret = readStream(in);
        } catch (IllegalStateException e) {
            Log.e(API_LOG_TAG, "Error reading stream");
        } catch (IOException e) {
            Log.e(API_LOG_TAG, "Error reading stream");
        }
    }

    return ret;
}

From source file:com.mycompany.trader.HTTPTransport.java

public static String sendGetReq(HttpGet data) {
    try {//  w  ww  . j a  v  a2 s  .  c om

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpResponse response = httpClient.execute(data);
        StringBuilder buffer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output;
        while ((output = br.readLine()) != null) {
            buffer.append(output);
        }
        System.out.println("response from server:");
        System.out.println(buffer.toString());
        if (response.getStatusLine().getStatusCode() != 200
                && response.getStatusLine().getStatusCode() != 201) {
            throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        httpClient.getConnectionManager().shutdown();
        return buffer.toString();

    } catch (Exception e) {
        return null;
    }

}

From source file:com.mycompany.trader.HTTPTransport.java

public static String sendData(HttpPost data, String header) throws Exception {
    try {// w  ww .j  a v  a  2s  .  co m

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpResponse response = httpClient.execute(data);
        StringBuilder buffer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output;
        while ((output = br.readLine()) != null) {
            buffer.append(output);
        }
        System.out.println("response from server:");
        System.out.println(buffer.toString());
        if (response.getStatusLine().getStatusCode() != 200
                && response.getStatusLine().getStatusCode() != 201) {
            buffer.append("error_code:" + response.getStatusLine().getStatusCode());
        }
        httpClient.getConnectionManager().shutdown();

        return buffer.toString();

    } catch (Exception e) {
        //e.printStackTrace();
        throw new Exception("server returned code: " + e.getMessage());
    }

}