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:cn.ctyun.amazonaws.regions.RegionUtils.java

/**
 * Fetches a file from the URL given and returns an input stream to it.
 *///from  ww w  .j  av  a2  s. c  o m
private static InputStream fetchFile(String url)
        throws IOException, ClientProtocolException, FileNotFoundException {

    HttpParams httpClientParams = new BasicHttpParams();
    HttpProtocolParams.setUserAgent(httpClientParams, VersionInfoUtils.getUserAgent());
    HttpClient httpclient = new DefaultHttpClient(httpClientParams);
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        return entity.getContent();
    }
    return null;
}

From source file:com.stratio.ingestion.utils.IngestionUtils.java

public static String getAgentStatus(int port) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://localhost:".concat(String.valueOf(port)).concat("/metrics"));
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuilder sb = new StringBuilder();
    String line;/*from ww w  .  j  av a  2 s  .co  m*/
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    return sb.toString();
}

From source file:org.wikipathways.client.utils.Utils.java

public static Document connect(String url, HttpClient client) throws IOException, JDOMException {
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = client.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream instream = entity.getContent();
    try {/*from  w  ww. j  a  v a2s  . c  o m*/
        String content = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
        String line;
        while ((line = reader.readLine()) != null) {
            content = content + line + "\n";
        }
        reader.close();

        SAXBuilder jdomBuilder = new SAXBuilder();
        Document jdomDocument = jdomBuilder.build(new StringReader(content));
        return jdomDocument;
    } finally {
        instream.close();
    }
}

From source file:com.liferay.mobile.android.http.file.UploadUtil.java

public static JSONArray upload(Session session, JSONObject command) throws Exception {

    String path = (String) command.keys().next();
    JSONObject parameters = command.getJSONObject(path);

    HttpClient client = getClient(session);
    HttpPostHC4 request = getHttpPost(session, getURL(session, path));

    HttpEntity entity = getMultipartEntity(request, parameters);

    request.setEntity(entity);/*from   ww  w.j av  a  2  s . c  om*/

    HttpResponse response = client.execute(request);
    String json = getResponseString(response);

    handleServerError(request, response, json);

    return new JSONArray("[" + json + "]");
}

From source file:com.welocalize.dispatcherMW.client.Main.java

public static String checkJobStaus(String p_jobId) {
    String url = getFunctinURL(TYPE_CHECK_STATUS, null, p_jobId);
    HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    try {/*from  w w w. j av  a2 s .c  om*/
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity resEntity = response.getEntity();
        String msg = EntityUtils.toString(resEntity);
        if (msg.startsWith("{\"status\":\"")) {
            return msg.substring(11, msg.indexOf(",") - 1);
        } else {
            System.out.println(msg);
        }
    } catch (Exception e) {
    } finally {
        httpget.releaseConnection();
    }

    return "";
}

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

public static void updateJob(PhotoSerie ps) {

    lastStatus = SmartPhoneStatus.TRYING;
    try {/*www . java2  s  .c  o  m*/
        HttpPost httppost = new HttpPost(
                PROTOCOL + "://" + LAST_WORKING_IP + ":" + PORT + "/" + WebService.WebCommands.updateJob);
        List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
        parameters.add(new BasicNameValuePair(WebService.WebParameters.jobid.name(), ps.getId()));

        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();

        // Print the response code and message body
        System.out.println("HTTP Status Code: " + statusCode);
        System.out.println(responseBody);

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

From source file:org.ado.biblio.desktop.BulkBookLoad.java

private static void send(String isbnUrl) throws IOException {
    final String isbn = isbnUrl.split("/")[5];
    HttpClient client = HttpClientBuilder.create().build();
    String url = String.format("%s/books/%s?format=%s&code=%s", "http://localhost:8086",
            "fd9728f8-e611-4e40-8c3d-6b959d265d62", "EAN_13", isbn);

    HttpPost httpPost = new HttpPost(url);
    client.execute(httpPost);
}

From source file:og.android.tether.system.WebserviceTask.java

public static Properties queryForProperty(String url) {
    Properties properties = null;
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(String.format(url));
    try {//from  w ww  .  jav  a2 s.c om
        HttpResponse response = client.execute(request);

        StatusLine status = response.getStatusLine();
        Log.d(MSG_TAG, "Request returned status " + status);
        if (status.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            properties = new Properties();
            properties.load(entity.getContent());
        }
    } catch (IOException e) {
        Log.d(MSG_TAG, "Can't get property '" + url + "'.");
    }
    return properties;
}

From source file:com.deployd.Deployd.java

public static JSONObject put(JSONObject input, String uri)
        throws ClientProtocolException, IOException, JSONException {

    HttpPut post = new HttpPut(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(input.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}

From source file:com.deployd.Deployd.java

public static JSONObject post(JSONObject input, String uri)
        throws ClientProtocolException, IOException, JSONException {

    HttpPost post = new HttpPost(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(input.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}