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:Main.java

public static JSONObject callService(String url) {
    InputStream inputStream = null;
    JSONObject result = null;/*from  w ww.j  a va 2  s  .c  o m*/
    try {
        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String response = convertInputStreamToString(inputStream);
            //result = new JSONObject(response);
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
    return result;
}

From source file:Main.java

public static byte[] loadByteFromURL(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;//from www . ja v a 2 s  . c  om
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return EntityUtils.toByteArray(httpEntity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static InputStream loadFileFromURL(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;// ww  w  . jav a 2 s  .co  m
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return httpEntity.getContent();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String loadTextFromURL(String url, String charset) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;/*from   w  w w  . j a v  a  2  s. co  m*/
    try {
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            return EntityUtils.toString(httpEntity, charset);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.trpr.platform.batch.impl.job.ha.service.FileUpload.java

/**
 * A generic method to execute any type of Http Request and constructs a response object
 * @param requestBase the request that needs to be exeuted
 * @return server response as <code>String</code>
 *//*from  www .  j  a v  a  2  s  . c o m*/
public static String executeRequest(HttpRequestBase requestBase) {
    //The string holding the server response
    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("Exception while uploading file to server", e);
    }
    client.getConnectionManager().shutdown();
    return responseString;
}

From source file:wvw.utils.pc.HttpHelper.java

public static String post(String url, JSONArray headers, String data) {
    HttpPost httpPost = new HttpPost(url);

    for (int i = 0; i < headers.length(); i++) {
        JSONArray header = headers.getJSONArray(i);

        httpPost.addHeader(header.getString(0), header.getString(1));
    }/* ww w  .jav a  2  s  .  c o  m*/

    String ret = null;
    try {
        httpPost.setEntity(new StringEntity(data));

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(httpPost);

        StatusLine status = response.getStatusLine();

        switch (status.getStatusCode()) {

        case 200:
            ret = IOUtils.readFromStream(response.getEntity().getContent());

            break;

        default:
            ret = genError(status.getReasonPhrase());

            break;
        }

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

        ret = genError(e.getClass() + ": " + e.getMessage());
    }

    return ret;
}

From source file:core.RESTCalls.RESTGet.java

public static String httpGet(String urlStr) throws Exception {

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(urlStr);

    HttpResponse response = client.execute(get);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    String content = "", line = null;

    while ((line = rd.readLine()) != null)
        content += line + "\n";

    return content;
}

From source file:com.example.qrpoll.MyHttpURLConnection.java

/**
 * pobieranie zawartosci strony z serwera
 * @param url adres url strony z ktora chcemy sie polaczyc
 * @return zawartosc strony//from   w w  w  .j  a v a 2 s. c  o m
 * @throws ClientProtocolException
 * @throws IOException
 * @throws Exception404 
 */
static public String get(String url) throws ClientProtocolException, IOException, Exception404 {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    int code = response.getStatusLine().getStatusCode();
    if (code == 404)
        throw new Exception404();

    InputStream is = entity.getContent();
    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();
    String resString = sb.toString();

    return resString;
}

From source file:com.klinker.android.dream.util.NetworkUtils.java

/**
 * Get a JSON string from the given url//  w  w  w  .  j a  va 2  s  .  c  o m
 * @param url the url where the JSON is located
 * @return the String in the format of a JSON
 */
public static String getJsonString(String url) {
    InputStream inputStream;
    String result = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        inputStream = httpResponse.getEntity().getContent();
        if (inputStream != null) {
            result = convertInputStreamToString(inputStream);
        } else {
            result = "Did not work!";
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

From source file:org.npr.api.HttpHelper.java

/**
 * A helper function to grab content from a URL.
 *
 * @param url URL of the item to download
 *
 * @return an input stream to the content. The caller is responsible for
 * closing the stream. Content will be null in the case of errors.
 * @throws java.io.IOException if an error occurs loading the url
 *///from  ww  w. j av a2 s . c  o  m
public static InputStream download(String url) throws IOException {
    InputStream data = null;
    Log.d(LOG_TAG, "Starting download: " + url);
    HttpClient http = new DefaultHttpClient();
    HttpGet method = new HttpGet(url);

    try {
        HttpResponse response = http.execute(method);
        data = response.getEntity().getContent();
    } catch (IllegalStateException e) {
        Log.e(LOG_TAG, "error downloading", e);
    }
    Log.d(LOG_TAG, "Download complete");
    return data;
}