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:eu.musesproject.windowsclient.contextmonitoring.sensors.RESTController.java

public static Map<String, String> requestSensorInfo(String sensorType) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://localhost:9000/api/" + sensorType);
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String responseData = "";
    Gson gson = new Gson();
    //ToDo:check for readLine function
    responseData = rd.readLine();/*from  ww  w.  j  a  va  2  s  .  c o m*/

    return gson.fromJson(responseData, new HashMap<String, String>().getClass());
}

From source file:org.outburstframework.test.util.http.HTTPUtils.java

/**
 * Makes a HTTP Get request.//from  www .  j  av a 2s . c  o m
 * 
 * @param url
  *          The URL fo the page you would like to return
 * @return The body of the HTTP request
 * @throws HTTPException
  *              The was a problem making the request
 * @throws InvalidResponseException
 *             The Response HTTP Status was not 200 OK
 */
public static String HTTPGet(String url) throws InvalidResponseException, HTTPException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);

    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        throw new HTTPException(e.getMessage(), e);
    }

    if (response.getStatusLine().toString().indexOf("200 OK") == -1) {
        throw new InvalidResponseException("Invalid status: " + response.getStatusLine());
    }

    try {
        return EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        throw new HTTPException(e.getMessage(), e);
    }
}

From source file:com.mothsoft.alexis.util.NetworkingUtil.java

public static HttpClientResponse post(final URL url, final List<NameValuePair> params) throws IOException {
    final HttpPost post = new HttpPost(url.toExternalForm());

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
    post.setEntity(formEntity);//from   w  ww .j  a  va  2s  .  c  o  m

    post.addHeader("Accept-Charset", "UTF-8");

    final HttpClient client = getClient();
    HttpResponse response = client.execute(post);
    int status = response.getStatusLine().getStatusCode();

    if (status != 200) {
        throw new IOException("status: " + status);
    }

    final HttpEntity entity = response.getEntity();
    final InputStream is = entity.getContent();
    final Charset charset = getCharset(entity);

    return new HttpClientResponse(post, status, null, null, is, charset);
}

From source file:org.jboss.teiid.quickstart.PortfolioHTTPClient.java

private static void execute(HttpClient client, HttpRequestBase httppost) {

    System.out.println("\n" + httppost.getRequestLine());

    HttpResponse response = null;/*www  .j  av  a 2s  .  c  om*/
    try {
        response = client.execute(httppost);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output;
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.devdom.commons.util.Utils.java

public static InputStream httpGet(String url, Map<String, String> headers) throws Exception {
    InputStream result = null;/*from   w w w. j a  v  a 2s.com*/
    HttpGet get = new HttpGet(url);

    if (headers != null) {
        for (Entry<String, String> header : headers.entrySet()) {
            get.addHeader(header.getKey(), header.getValue());
        }
    }

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse httpResponse = client.execute(get);

    if (httpResponse.getEntity() != null) {
        result = httpResponse.getEntity().getContent();
    }

    return result;
}

From source file:org.ptlug.ptwifiauth.Utilities.java

public static boolean testConnection() {
    /*//from   w w  w  .  java2 s .co  m
     * if(GlobalSpace.usr_sess.getDefaultUrl() == 0) return true; else
     * if(GlobalSpace.usr_sess.getDefaultUrl() == 4) return true; else
     * if(GlobalSpace.usr_sess.getDefaultUrl() == 1) return false; //SET
     * 'true' ONLY FOR TESTING else return false;
     */
    boolean result = false;
    try {
        HttpGet request = new HttpGet(AppConsts.ping_url);

        HttpParams httpParameters = new BasicHttpParams();

        // HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
        HttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpResponse response = httpClient.execute(request);

        int status = response.getStatusLine().getStatusCode();

        if (status == HttpStatus.SC_OK) {
            result = true;
        }
        return result;

    } catch (Exception e) {
        result = false;
    }
    /*
     * catch (SocketTimeoutException e) { result = false; // this is
     * somewhat expected } catch (ClientProtocolException e) { result =
     * false; } catch (IOException e) { result = false;; }
     */
    return result;
}

From source file:downloadwolkflow.getWorkFlowList.java

private static void writeToFile(String downloadUrl, HttpClient httpclient) throws IOException {
    HttpGet httpget = new HttpGet(downloadUrl);
    HttpResponse response = httpclient.execute(httpget);
    String xml = EntityUtils.toString(response.getEntity());
    String filename = downloadUrl.split("/")[downloadUrl.split("/").length - 1].split("\\?")[0];
    System.out.println(filename);
    try (PrintWriter out = new PrintWriter("data/" + filename)) {
        out.println(xml);//w  w w  .j av  a2s .  co  m
    }
}

From source file:com.alta189.cyborg.commandkit.util.HttpUtil.java

public static String readRandomLine(String url) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();
    try {//from  w w  w.j  a v  a  2 s  .c o m
        HttpResponse response = httpClient.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        List<String> lines = new ArrayList<String>();
        String line = "";
        while ((line = rd.readLine()) != null) {
            lines.add(line);
        }

        if (lines.size() > 1) {
            int i = randomNumber(0, lines.size() + 1);
            return lines.get(i);
        } else if (lines.size() == 1) {
            return lines.get(0);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.infstudio.inflauncher.game.downloading.URLConnectionTimer.java

public static long testTime(String url) {
    try {/*from  ww w . ja v a 2 s.  co m*/
        HttpClient client = HttpClientBuilder.create().build();
        HttpUriRequest request = RequestBuilder.get(url).build();
        long start = System.currentTimeMillis();
        client.execute(request);
        long end = System.currentTimeMillis();
        return end - start;
    } catch (IOException e) {
        InfinityDownloader.logger.error(e);
        return Long.MAX_VALUE;
    }
}

From source file:com.Kuesty.services.JSONRequest.java

public static void execute(String uri, JSONRequestTaskHandler rsh) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;//from  w  ww.j ava 2 s  .  co m
    String responseString = null;

    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

        } else {
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        rsh.onError(e.getMessage());
    } catch (IOException e) {
        rsh.onError(e.getMessage());
    }
    try {
        JSONObject response1 = new JSONObject(responseString);
        rsh.onSuccess(response1);

    } catch (JSONException e) {
        rsh.onError(e.getMessage());
    }
}