Example usage for org.apache.http.impl.client DefaultHttpClient execute

List of usage examples for org.apache.http.impl.client DefaultHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:org.mustard.util.MustardUtil.java

public static void snapshot(String id, String version, String accountNumber) {
    try {//from  w w  w .  j  a  v a 2s.c o  m
        HttpPost post = new HttpPost("http://mustard.macno.org/snapshot.php");
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("v", version));
        params.add(new BasicNameValuePair("n", accountNumber));
        params.add(new BasicNameValuePair("m", id));
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        DefaultHttpClient hc = new DefaultHttpClient();
        HttpProtocolParams.setUserAgent(post.getParams(), "Mustard/1.0");
        HttpProtocolParams.setUseExpectContinue(post.getParams(), false);
        hc.execute(post);
    } catch (Exception e) {
    }
}

From source file:com.shafiq.mytwittle.URLFetch.java

public static InputStream retrieveStream(String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(url);
    try {//from   ww  w. j  a  va2s  . com
        HttpResponse getResponse = client.execute(getRequest);
        final int statusCode = getResponse.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("abtApp", "Error " + statusCode + " for URL " + url);
            return null;
        }

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();
    } catch (IOException e) {
        getRequest.abort();
        Log.w("abtApp", "Error for URL " + url, e);
    }
    return null;
}

From source file:CB_Core.GCVote.GCVote.java

private static String Execute(HttpRequestBase httprequest) throws IOException, ClientProtocolException {
    httprequest.setHeader("Content-type", "application/x-www-form-urlencoded");
    // httprequest.setHeader("UserAgent", "cachebox");

    int conectionTimeout = CB_Core_Settings.conection_timeout.getValue();
    int socketTimeout = CB_Core_Settings.socket_timeout.getValue();

    // Execute HTTP Post Request
    String result = "";

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.

    HttpConnectionParams.setConnectionTimeout(httpParameters, conectionTimeout);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.

    HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    HttpResponse response = httpClient.execute(httprequest);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String line = "";
    while ((line = rd.readLine()) != null) {
        result += line + "\n";
    }/*from   ww  w.j  a  v  a2  s. c  om*/
    return result;

}

From source file:com.lambdasoup.panda.PandaHttp.java

public static void delete(String url, HashMap<String, String> params, Properties properties) {
    Map<String, String> sParams = signedParams("DELETE", url, params, properties);
    String flattenParams = canonicalQueryString(sParams);
    String requestUrl = "http://" + properties.getProperty("api-host") + ":80/v2" + url + "?" + flattenParams;
    HttpDelete httpDelete = new HttpDelete(requestUrl);
    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {/*from  w ww .j av  a 2s. co m*/
        httpclient.execute(httpDelete);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.zywx.wbpalmstar.engine.eservice.EServiceTest.java

public static void test() {
    String realyPath = "http://localhost:8000/other.QDV";
    HttpRequestBase mHhttpRequest = new HttpGet(realyPath);
    mHhttpRequest.addHeader("range", "bytes=34199-");
    BasicHttpParams bparams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(bparams, 20000);
    HttpConnectionParams.setSoTimeout(bparams, 20000);
    HttpConnectionParams.setSocketBufferSize(bparams, 8 * 1024);
    HttpClientParams.setRedirecting(bparams, true);
    DefaultHttpClient mDefaultHttpClient = new DefaultHttpClient(bparams);
    HttpResponse response = null;/*from  w  w w  .  ja v a 2s.co  m*/
    try {
        response = mDefaultHttpClient.execute(mHhttpRequest);

        int responseCode = response.getStatusLine().getStatusCode();
        byte[] arrayOfByte = null;
        HttpEntity httpEntity = response.getEntity();
        if (responseCode == 200 || responseCode == 206) {
            arrayOfByte = toByteArray(httpEntity);
            String m = new String(arrayOfByte, "UTF-8");
            Log.i("ldx", "" + m.length());
            Log.i("ldx", m);
            return;

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

From source file:neembuu.release1.httpclient.utils.NHttpClientUtils.java

/**
 * Calculate the length//www  .  j av  a2 s .  c  o m
 * @param url
 * @param httpClient
 * @return the length
 */
public static long calculateLength(String url, DefaultHttpClient httpClient) {
    try {
        //DefaultHttpClient httpClient = NHttpClient.getInstance();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet);
        long length = response.getEntity().getContentLength();
        System.out.println("Length: " + length);
        httpGet.abort();
        return length;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return -1;
}

From source file:neembuu.release1.httpclient.utils.NHttpClientUtils.java

/**
 * Get the content type of a url.//from ww  w  .  j  a va  2s.  c  o m
 * @param url
 * @return the content type.
 */
public static String getContentType(String url, DefaultHttpClient httpClient) {
    try {
        //DefaultHttpClient httpClient = NHttpClient.getInstance();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet);
        String contentType = response.getEntity().getContentType().getValue();
        System.out.println("Content Type: " + contentType);
        EntityUtils.consume(response.getEntity());
        return contentType;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.melchor629.musicote.Utils.java

public static ArrayList getHashMapFromUrl(String url) {
    //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    //StrictMode.setThreadPolicy(policy);
    HashMap map = new HashMap();
    try {//from ww w  .  java  2 s .  c o m
        long time = System.currentTimeMillis();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream is = httpEntity.getContent();

        //BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"), 8192);
        StringBuilder sb = new StringBuilder();
        byte[] buff = new byte[256];
        int length = is.read(buff);
        sb.append(new String(buff, 0, length));
        while ((length = is.read(buff)) != -1) {
            sb.append(new String(buff, 0, length));
        }
        is.close();
        Log.d("MainActivity", String.format("JSON Downloaded in %dms", System.currentTimeMillis() - time));

        time = System.currentTimeMillis();
        Gson gson = new Gson();
        String s = sb.toString();
        map = gson.fromJson(s, map.getClass());
        Log.d("MainActivity", String.format("JSON parsed in %dms", System.currentTimeMillis() - time));
        return (ArrayList) map.get("canciones");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        Log.e("Utils", "El archivo recibido no es json");
    }
    return null;
}

From source file:se.vgregion.util.HTTPUtils.java

public static HttpResponse makeRequest(String url, String token, DefaultHttpClient client) throws Exception {
    HttpGet get = new HttpGet(url);
    get.addHeader("X-TrackerToken", token);
    return client.execute(get);
}

From source file:com.lambdasoup.panda.PandaHttp.java

static String postFile(String url, Map<String, String> params, Properties properties, File file) {
    Map<String, String> sParams = signedParams("POST", url, params, properties);
    String flattenParams = canonicalQueryString(sParams);
    String requestUrl = "http://" + properties.getProperty("api-host") + ":80/v2" + url + "?" + flattenParams;

    HttpPost httpPost = new HttpPost(requestUrl);
    String stringResponse = null;
    FileBody bin = new FileBody(file, "application/octet-stream");
    try {//from  www. j  a v a  2  s. c  o  m
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", bin);

        httpPost.setEntity(entity);

        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpResponse response = httpclient.execute(httpPost);
        stringResponse = EntityUtils.toString(response.getEntity());

    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringResponse;
}