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:com.jkoolcloud.tnt4j.streams.fields.ActivityCacheTest.java

private static void sendRequest(HttpClient client, String file) throws Exception {
    URI url = makeURI();/*from ww w.ja va  2  s.  c  o m*/
    HttpPost post = new HttpPost(url);

    post.setEntity(EntityBuilder.create().setText(getFileContents(file)).build());
    final HttpResponse returned = client.execute(post);
}

From source file:com.wms.opensource.images3android.images3.ImageS3Service.java

private static String getResponse(String url) {
    HttpClient client = getClient();
    HttpGet httpGet = new HttpGet(url);
    httpGet.addHeader("Accept", "application/json");
    HttpResponse response;//www  . j  a  v  a 2 s.  c o m
    try {
        response = client.execute(httpGet);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            if (response.getEntity() != null) {
                HttpEntity resEntity = response.getEntity();
                return EntityUtils.toString(resEntity);
            }
        }
    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }

    return null;
}

From source file:com.websqrd.catbot.scraping.HttpHandler.java

public static String executeGet(HttpClient httpclient, String url, StorableInputStream is) throws IOException {
    String type = "text";
    HttpGet httget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httget);
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentType();
    if (header != null) {
        type = header.getValue().toLowerCase();
    }//from   ww w .j a  v a  2 s .  c  o m
    is.addStream(entity.getContent());
    is.rewind();
    return type;
}

From source file:de.quadrillenschule.azocamsyncd.astromode.SmartPhoneWrapper.java

public static void addJob(PhotoSerie ps) throws IOException {

    lastStatus = SmartPhoneStatus.TRYING;
    try {/*from   w  w  w  .j av  a2s.  com*/
        HttpPost httppost = new HttpPost(
                PROTOCOL + "://" + LAST_WORKING_IP + ":" + PORT + "/" + WebService.WebCommands.addjob);
        List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
        parameters.add(new BasicNameValuePair(WebService.WebParameters.jsoncontent.name(),
                ps.toJSONObject().toString()));

        httppost.setEntity(new UrlEncodedFormEntity(parameters));

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();

        // Get the HTTP Status Code
        int statusCode = httpResponse.getStatusLine().getStatusCode();

        // Get the contents of the response
        InputStream input = resEntity.getContent();
        String responseBody = IOUtils.toString(input);
        input.close();

        lastStatus = SmartPhoneStatus.CONNECTED;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(SmartPhoneWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.iaraby.utility.HttpUtility.java

/**
 * Execute Http GET connection with the server and receive the response from server
 * //from www  .  j a  v a  2 s .  c om
 * @param HttpClient client
 * @param String link
 * @return server response as String value
 * @throws ClientProtocolException
 * @throws IOException if the returned code form server != 200
 */
public static String executeGetHttpCommand(HttpClient client, String link)
        throws ClientProtocolException, IOException {

    HttpGet get = new HttpGet(link);

    HttpResponse response = client.execute(get);

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode != 200) {
        throw new IOException("Http error code :" + statusCode);
    }

    //read response using http util.
    String data = readHttpResponse(response);

    return data;
}

From source file:com.microsoft.azure.hdinsight.spark.jobs.SparkRestUtil.java

public static HttpEntity getEntity(@NotNull IClusterDetail clusterDetail, @NotNull String restUrl)
        throws HDIException, IOException {
    provider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(clusterDetail.getHttpUserName(), clusterDetail.getHttpPassword()));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
    String url = String.format(SPARK_REST_API_ENDPOINT, clusterDetail.getName(), restUrl);
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    int code = response.getStatusLine().getStatusCode();
    if (code == HttpStatus.SC_OK || code == HttpStatus.SC_CREATED) {
        return response.getEntity();
    } else {// w  ww .  ja  va2s  .  c  o  m
        throw new HDIException(response.getStatusLine().getReasonPhrase(),
                response.getStatusLine().getStatusCode());
    }
}

From source file:com.rogiel.httpchannel.util.HttpClientUtils.java

public static Future<String> executeAsync(final HttpClient client, final HttpUriRequest request)
        throws IOException {
    return threadPool.submit(new Callable<String>() {
        @Override/*from   w  w  w.  j  a v  a  2  s .  c om*/
        public String call() throws Exception {
            return HttpClientUtils.toString(client.execute(request));
        }
    });
}

From source file:uk.org.todome.Util.java

public static String getFileFromServer(String request) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(request);
    Log.i("Util.getFileFromServer", "Request used: " + request);
    try {/*w w  w .j  a v a2 s . c  o m*/
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("", "Failed to download file");
        }
    } catch (Exception ex) {
        Log.e("Util.getFileFromServer", ex.getClass().toString() + " " + ex.getMessage());
    }

    return builder.toString();
}

From source file:com.impetus.ankush.agent.action.impl.UploadHandler.java

/**
 * A generic method to execute any type of Http Request and constructs a
 * response object.//  w  w  w  . j  av a2  s.co  m
 * 
 * @param requestBase
 *            the request that needs to be exeuted
 * @return server response as <code>String</code>
 */
private static String executeRequest(HttpRequestBase requestBase) {
    String responseString = "";

    InputStream responseStream = null;
    HttpClient client = new DefaultHttpClient();
    try {
        HttpResponse response = client.execute(requestBase);
        if (response != null) {
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {
                responseStream = responseEntity.getContent();
                if (responseStream != null) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
                    String responseLine = br.readLine();
                    String tempResponseString = "";
                    while (responseLine != null) {
                        tempResponseString = tempResponseString + responseLine
                                + System.getProperty("line.separator");
                        responseLine = br.readLine();
                    }
                    br.close();
                    if (tempResponseString.length() > 0) {
                        responseString = tempResponseString;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    } finally {
        if (responseStream != null) {
            try {
                responseStream.close();
            } catch (IOException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
    client.getConnectionManager().shutdown();

    return responseString;
}

From source file:org.artags.android.app.stackwidget.util.HttpUtils.java

public static String getUrl(String url) {
    String result = "";
    HttpClient httpclient = new DefaultHttpClient();

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

    try {
        response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();
            result = convertStreamToString(instream);
        }
    } catch (IOException ex) {
        Log.e(HttpUtils.class.getName(), ex.getMessage());
    }
    return result;
}