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.teleca.jamendo.api.util.Caller.java

/**
 * Performs HTTP GET using Apache HTTP Client v 4
 * /*from   ww w.ja  v  a  2  s  . c o m*/
 * @param url
 * @return
 * @throws WSError 
 */
public static String doGet(String url) throws WSError {

    String data = null;
    if (requestCache != null) {
        data = requestCache.get(url);
        if (data != null) {
            Log.d(JamendoApplication.TAG, "Caller.doGet [cached] " + url);
            return data;
        }
    }

    URI encodedUri = null;
    HttpGet httpGet = null;

    try {
        encodedUri = new URI(url);
        httpGet = new HttpGet(encodedUri);
    } catch (URISyntaxException e1) {
        // at least try to remove spaces
        String encodedUrl = url.replace(' ', '+');
        httpGet = new HttpGet(encodedUrl);
        e1.printStackTrace();
    }

    // initialize HTTP GET request objects
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse;

    try {
        // execute request
        try {
            httpResponse = httpClient.execute(httpGet);
        } catch (UnknownHostException e) {
            throw new WSError("Unable to access " + e.getLocalizedMessage());
        } catch (SocketException e) {
            throw new WSError(e.getLocalizedMessage());
        }

        // request data
        HttpEntity httpEntity = httpResponse.getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            data = convertStreamToString(inputStream);
            // cache the result
            if (requestCache != null) {
                requestCache.put(url, data);
            }
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(JamendoApplication.TAG, "Caller.doGet " + url);
    return data;
}

From source file:net.dian1.player.api.util.Caller.java

/**
 * Performs HTTP GET using Apache HTTP Client v 4
 * /* ww  w  . j  a  v a2s  .c o m*/
 * @param url
 * @return
 * @throws WSError 
 */
public static String doGet(String url) throws WSError {

    String data = null;
    if (requestCache != null) {
        data = requestCache.get(url);
        if (data != null) {
            Log.d(Dian1Application.TAG, "Caller.doGet [cached] " + url);
            return data;
        }
    }

    URI encodedUri = null;
    HttpGet httpGet = null;

    try {
        encodedUri = new URI(url);
        httpGet = new HttpGet(encodedUri);
    } catch (URISyntaxException e1) {
        // at least try to remove spaces
        String encodedUrl = url.replace(' ', '+');
        httpGet = new HttpGet(encodedUrl);
        e1.printStackTrace();
    }

    // initialize HTTP GET request objects
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse;

    try {
        // execute request
        try {
            httpResponse = httpClient.execute(httpGet);
        } catch (UnknownHostException e) {
            throw new WSError("Unable to access " + e.getLocalizedMessage());
        } catch (SocketException e) {
            throw new WSError(e.getLocalizedMessage());
        }

        // request data
        HttpEntity httpEntity = httpResponse.getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            data = convertStreamToString(inputStream);
            // cache the result
            if (requestCache != null) {
                requestCache.put(url, data);
            }
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(Dian1Application.TAG, "Caller.doGet " + url);
    return data;
}

From source file:com.ibm.iot.iotspark.IoTPrediction.java

/**
 * Makes ReST call to the Predictive Analytics service with the given payload and responds with the predicted score.
 * /*from  w  ww .ja v a2 s.c o m*/
 * @param pURL
 * @param payload
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public static java.lang.String post(java.lang.String pURL, java.lang.String payload)
        throws ClientProtocolException, IOException {

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(pURL);
    StringEntity input = new StringEntity(payload);
    input.setContentType("application/json");
    post.setEntity(input);

    HttpResponse response = client.execute(post);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuilder out = new StringBuilder();
    java.lang.String line;
    while ((line = rd.readLine()) != null) {
        //    System.out.println(line);
        out.append(line);
    }

    System.out.println(out.toString()); //Prints the string content read from input stream
    rd.close();
    return out.toString();
}

From source file:org.wso2.carbon.appmgt.sample.deployer.http.HttpHandler.java

/**
 * This method is use get a html file for given url
 *
 * @param url Web page url// ww w.  j  a v  a 2s  . com
 * @return response
 * @throws java.io.IOException Throws this when failed to retrieve web page
 */
public static String getHtml(String url) throws IOException {
    HttpClient httpClient = AppManagerUtil.getHttpClient(url);
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(content));
    StringBuffer responseBuffer = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
        responseBuffer.append(line);
    }
    if (in != null) {
        in.close();
    }
    return responseBuffer.toString();
}

From source file:com.storageroomapp.client.util.Http.java

/**
 * GET the payload for the given URL as an Inputstream
 * /*from   w ww.j  av a  2 s.co m*/
 * NOTE: caller must close the stream
 * @param url a String url
 * @return an Inputstream if successful, null otherwise
 */
static public InputStream get(String url) {
    InputStream instream = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        int code = response.getStatusLine().getStatusCode();
        String reason = response.getStatusLine().getReasonPhrase();
        if (entity != null) {
            instream = entity.getContent();
            if (log.isDebugEnabled()) {
                log.debug("Http.get url [" + url + "] response code [" + code + "] reason [" + reason
                        + "] body [input stream]");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Http.get url [" + url + "] response code [" + code + "] reason [" + reason
                        + "] body [nothing returned]");
            }
        }
    } catch (Exception e) {
        log.error("Http.get failed, with url [" + url + "]", e);
    }
    return instream;
}

From source file:com.storageroomapp.client.util.Http.java

/**
 * GET the payload for the given URL as a String
 * /* w ww  .  ja v  a  2s.  co m*/
 * @param url a String url
 * @return a String if successful, null otherwise
 */
static public String getAsString(String url) {
    String body = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        int code = response.getStatusLine().getStatusCode();
        String reason = response.getStatusLine().getReasonPhrase();
        if (entity != null) {
            InputStream instream = entity.getContent();
            body = deserializeBody(instream);
            if (log.isDebugEnabled()) {
                log.debug("Http.getAsString url [" + url + "] response code [" + code + "] reason [" + reason
                        + "] body [" + body + "]");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Http.getAsString url [" + url + "] response code [" + code + "] reason [" + reason
                        + "] body [nothing returned]");
            }
        }
    } catch (Exception e) {
        log.error("Http.getAsString failed, with url [" + url + "]", e);
    }
    return body;
}

From source file:com.storageroomapp.client.util.Http.java

/**
 * DELETE the url/* w ww  .ja va 2  s.  co m*/
 * @param url a String url
 * @return true if successful (response code < 400), false otherwise
 */
static public boolean delete(String url) {
    boolean success = false;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpDelete httpdelete = new HttpDelete(url);
        HttpResponse response = httpclient.execute(httpdelete);
        int code = response.getStatusLine().getStatusCode();
        success = code < 400;

        if (log.isDebugEnabled()) {
            String reason = response.getStatusLine().getReasonPhrase();
            log.debug("Http.delete url [" + url + "] response code [" + code + "] reason [" + reason + "]");
        }

    } catch (Exception e) {
        log.error("Http.delete failed, with url [" + url + "]", e);
    }
    return success;
}

From source file:net.ccghe.utils.Server.java

public static JSONObject Send(PostDataPairs pairs) {
    HttpClient client = new DefaultHttpClient();
    try {/*from ww w .j av a 2 s. c om*/
        HttpPost post = new HttpPost(serverURL);
        post.setEntity(new UrlEncodedFormEntity(pairs.get()));
        HttpResponse response = client.execute(post);

        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        String jsonResponse = convertStreamToString(stream);
        stream.close();
        if (entity != null) {
            entity.consumeContent();
        }
        JSONObject jObject = new JSONObject(jsonResponse);

        return jObject;
    } catch (ClientProtocolException e) {
        Log.e("EMOCHA", "ClientProtocolException ERR. " + e.getMessage());
    } catch (UnknownHostException e) {
        Log.e("EMOCHA", "UnknownHostException ERR. " + e.getMessage());
    } catch (IOException e) {
        Log.e("EMOCHA", "IOException ERR. " + e.getMessage());
    } catch (Exception e) {
        Log.e("EMOCHA", "Exception ERR. " + e.getMessage());
    }
    return null;
}

From source file:simple.crawler.http.HttpClientUtil.java

public static String fetch(HttpClient httpclient, String uri) throws Exception {
    if (httpclient == null) {
        throw new NullPointerException();
    }//from  w ww  .j av a  2  s  .c  o  m
    if (uri == null) {
        throw new NullPointerException();
    }
    HttpGet get = new HttpGet(uri);
    return getContentBodyAsString(httpclient.execute(get));
}

From source file:com.appdynamics.openstack.nova.RestClient.java

static String doGet(String url, String token) throws Exception {
    HttpClient httpClient = httpClientWithTrustManager();

    HttpGet get = new HttpGet(url);
    get.addHeader("accept", xmlContentType);
    get.addHeader("X-Auth-Token", token);

    HttpResponse response = httpClient.execute(get);

    return getResponseString(httpClient, response);
}