Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

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

Prototype

HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context.

Usage

From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java

public static String simpleQuery(String url) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;//from w w  w .j  ava  2 s. c om

    response = httpclient.execute(httpget);
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
            bo.write(tmp, 0, l);
        }
    }

    return bo.toString();
}

From source file:eu.trentorise.smartcampus.portfolio.utils.NetUtility.java

public static Bitmap loadBitmapfromUrl(String imageUrl) {
    Bitmap resultImage = null;//from  w w w. ja  va 2s.  c  o m
    HttpGet getRequest = new HttpGet();
    try {
        URI imageURI = new URI(imageUrl);
        getRequest.setURI(imageURI);
        HttpClient httpClient = HttpClientFactory.INSTANCE.getThreadSafeHttpClient();
        HttpResponse response = httpClient.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(NetUtility.class.getName(), "Error: " + statusCode + " image url: " + imageUrl);
        } else {
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = new FlushedInputStream(entity.getContent());
                    resultImage = BitmapFactory.decodeStream(inputStream);
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        }
    } catch (Exception e) {
        getRequest.abort();
        e.printStackTrace();
        Log.w(NetUtility.class.getName(), "Error image url: " + imageUrl);
    }
    return resultImage;
}

From source file:Main.java

public static void get(String url) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    try {/*w  w w. j  a  v a2s  . com*/
        httpget.setHeader("Content-Type", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine());
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getMessage());
    }

}

From source file:com.splunk.shuttl.testutil.TUtilsMockito.java

/**
 * @return HttpClient that always random HttpStatus.
 *//*from  w  w w .  j a  va2 s  .com*/
public static HttpClient createRandomHttpStatusHttpClient() {
    HttpClient httpClient = mock(HttpClient.class);
    StatusLine statusLine = mock(StatusLine.class);
    try {
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(new BasicHttpResponse(statusLine));
    } catch (Exception e) {
        TUtilsTestNG.failForException("Could not assign return value for execute", e);
    }
    when(statusLine.getStatusCode()).thenReturn(getAnyHttpStatus());
    return httpClient;
}

From source file:org.jboss.as.test.clustering.ClusterHttpClientUtil.java

public static HttpResponse tryGet(final HttpClient client, final HttpUriRequest r) throws IOException {
    final long startTime;
    HttpResponse response = client.execute(r);
    startTime = System.currentTimeMillis();
    while (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK
            && startTime + GRACE_TIME_TO_REPLICATE > System.currentTimeMillis()) {
        response = client.execute(r);/*from   www  . j av a  2  s.  c om*/
    }
    return response;
}

From source file:com.matze5800.paupdater.JSONfunctions.java

public static JSONObject getJSONfromURL(String url) {

    InputStream is = null;/*from  ww  w  .  ja va  2  s  .com*/
    String result = "";
    JSONObject jArray = null;

    //http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());

    }

    //convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}

From source file:org.apache.bigtop.bigpetstore.qstream.Utils.java

public static HttpResponse get(URI uri) throws Exception {
    HttpGet httppost = new HttpGet(uri);
    HttpClient httpclient = HttpClients.createDefault();
    //Execute and get the response.
    try {//from  w  w w.j a  va 2s .c  o m
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() != 200)
            System.err.println("FAILURE! " + response.getStatusLine().getStatusCode());
        return response;
    } catch (Throwable t) {
        System.out.println("failed, sleeping");
        Thread.sleep(10000);
    }
    System.err.println("FAILURE getting URI " + uri.toASCIIString());
    return null;
}

From source file:tern.repository.TernRepositoryHelper.java

/**
 * Load tern modules coming from the given repository.json URL.
 * /*from ww  w  .ja v  a2s . c  o  m*/
 * @param repositoryURL
 *            repository URL.
 * @return
 * @throws IOException
 * @throws ClientProtocolException
 * @throws TernException
 */
public static List<ITernModule> loadModules(String repositoryURL) throws IOException, TernException {
    // load repository.json with HTTP client.
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(repositoryURL);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity entity = httpResponse.getEntity();
    InputStream in = entity.getContent();
    // Check the status
    StatusLine statusLine = httpResponse.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        String message = IOUtils.toString(in);
        if (StringUtils.isEmpty(message)) {
            throw new TernException(statusLine.toString());
        }
        throw new TernException(message);
    }

    // read JSON and create tern modules list
    JsonObject repository = JsonObject.readFrom(new InputStreamReader(in));
    ITernModule module = null;
    List<ITernModule> modules = new ArrayList<ITernModule>();
    for (Member member : repository) {
        module = new TernModuleToDownload(member.getName(), (JsonObject) member.getValue());
        modules.add(module);
    }
    return modules;
}

From source file:eu.sisob.uma.NPL.Researchers.Freebase.Utils.java

public static JsonObject doRESTCall(String url) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(url));

    JsonParser parser = new JsonParser();
    JsonObject json_data = (JsonObject) parser.parse(EntityUtils.toString(response.getEntity()));

    return json_data;
}

From source file:com.asakusafw.shafu.core.net.ShafuNetwork.java

private static <T> T processHttpContent(URL url, IContentProcessor<T> processor) throws IOException {
    HttpClient client = Activator.getHttpClient();
    HttpGet request = new HttpGet(url.toExternalForm());
    HttpResponse response = client.execute(request);
    try {//w w w .  j  a va2s .co m
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            return processor.process(response.getEntity().getContent());
        } else {
            throw new IOException(MessageFormat.format(Messages.ShafuNetwork_failedToOpenHttpContent,
                    request.getURI(), response.getStatusLine()));
        }
    } finally {
        closeQuietly(response);
    }
}