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

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

Introduction

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

Prototype

public synchronized final ClientConnectionManager getConnectionManager() 

Source Link

Usage

From source file:org.opf_labs.fmts.fidget.droid.PRONOMSigGenerator.java

/**
 * @param sigdef//from  www.  j ava  2  s  .  c o m
 */
public static void generatePRONOMSigFile(SigDefSubmission sigdef) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {

        HttpPost httpost = new HttpPost(SERVICE_URL);

        httpost.setEntity(new UrlEncodedFormEntity(createNameValuePairs(sigdef), Consts.UTF_8));

        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        // Print out:
        IOUtils.copy(entity.getContent(), System.out);
        // Finish up:
        EntityUtils.consume(entity);

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

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

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClient(Certificate[] sslCertificate) {
    DefaultHttpClient httpClient;

    httpClient = new DefaultHttpClient();
    try {/*from  ww  w.  j av  a 2s .com*/
        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:org.androidnerds.reader.util.api.Subscriptions.java

/**
 * This method queries Google Reader for the list of subscribed feeds.
 * //  ww  w  .  j a  v  a  2  s.c  om
 * @param sid authentication code to pass along in a cookie.
 * @return arr returns a JSONArray of JSONObjects for each feed.
 *
 * The JSONObject returned by the service looks like this:
 *    id: this is the feed url.
 *    title: this is the title of the feed.
 *    sortid: this has not been figured out yet.
 *    firstitemsec: this has not been figured out yet.
 */
public static JSONArray getSubscriptionList(String sid) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(SUB_URL + "/list?output=json");
    BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        HttpResponse response = client.execute(get);
        HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        InputStream in = respEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        JSONObject obj = new JSONObject(arr);
        JSONArray array = obj.getJSONArray("subscriptions");

        reader.close();
        client.getConnectionManager().shutdown();

        return array;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:ch.entwine.weblounge.test.util.TestSiteUtils.java

/**
 * Test for the correct response when modified since header is set.
 * //from  w  w  w  .j a v  a  2  s. c  o  m
 * @param request
 *          the http request
 * @param date
 *          the expected modification date
 * @param logger
 *          used to log test output
 * @param params
 *          the request parameters
 * @throws Exception
 *           if processing the request fails
 */
public static void testModifiedHeader(HttpUriRequest request, Date modificationDate, Logger logger,
        String[][] params) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    Date before = new Date(modificationDate.getTime() - Times.MS_PER_DAY);
    Date after = new Date(modificationDate.getTime() + Times.MS_PER_DAY);
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
    try {
        request.removeHeaders("If-None-Match");
        request.setHeader("If-Modified-Since", format.format(after));
        logger.info("Sending 'If-Modified-Since' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
        assertNull(response.getEntity());
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    httpClient = new DefaultHttpClient();
    try {
        request.removeHeaders("If-None-Match");
        request.setHeader("If-Modified-Since", format.format(before));
        logger.info("Sending 'If-Modified-Since' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
        assertNotNull(response.getEntity());
        response.getEntity().consumeContent();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:middleware.HTTPRequest.java

public static void doPostVuelo(String jsonRequest) throws UnsupportedEncodingException, IOException {
    String url = "http://localhost:8084/MVIv2/webapi/vuelos";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
    StringEntity input = new StringEntity(jsonRequest);
    input.setContentType("application/json");
    postRequest.setEntity(input);/*from w  w w.j a v  a2  s  .  c o m*/

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("ERROR AL INSERTAR DEL TIPO: " + response.getStatusLine().getStatusCode());
    }

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

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

}

From source file:middleware.HTTPRequest.java

public static void doPostReserva(String jsonRequest) throws UnsupportedEncodingException, IOException {
    String url = "http://localhost:8084/MVIv2/webapi/reservas";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
    StringEntity input = new StringEntity(jsonRequest);
    input.setContentType("application/json");
    postRequest.setEntity(input);// w  w w .  j  ava 2s.c  o m

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("ERROR AL INSERTAR DEL TIPO: " + response.getStatusLine().getStatusCode());
    }

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

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

}

From source file:org.androidnerds.reader.util.api.Authentication.java

/**
 * This method returns back to the caller a proper authentication token to use with
 * the other API calls to Google Reader.
 *
 * @param user - the Google username//from   www . j a v  a  2  s. c o m
 * @param pass - the Google password
 * @return sid - the returned authentication token for use with the API.
 *
 */
public static String getAuthToken(String user, String pass) {
    NameValuePair username = new BasicNameValuePair("Email", user);
    NameValuePair password = new BasicNameValuePair("Passwd", pass);
    NameValuePair service = new BasicNameValuePair("service", "reader");
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(username);
    pairs.add(password);
    pairs.add(service);

    try {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(AUTH_URL);
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Server Response: " + response.getStatusLine());

        InputStream in = respEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String result = null;

        while ((line = reader.readLine()) != null) {
            if (line.startsWith("SID")) {
                result = line.substring(line.indexOf("=") + 1);
            }
        }

        reader.close();
        client.getConnectionManager().shutdown();

        return result;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:org.neo4j.nlp.examples.author.main.java

private static String executePost(String targetURL, String payload) {
    try {//ww w.j a  va  2  s  . c  om

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(targetURL);

        StringEntity input = new StringEntity(payload);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

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

        StringBuilder output = new StringBuilder();
        while (br.read() != -1) {
            output.append(br.readLine()).append('\n');
        }

        httpClient.getConnectionManager().shutdown();

        return output.toString();

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;
}

From source file:com.quietlycoding.android.reader.util.api.Subscriptions.java

/**
 * This method queries Google Reader for the list of subscribed feeds.
 * /*  ww w  .  jav a 2  s  .c o  m*/
 * @param sid
 *            authentication code to pass along in a cookie.
 * @return arr returns a JSONArray of JSONObjects for each feed.
 * 
 *         The JSONObject returned by the service looks like this: id: this
 *         is the feed url. title: this is the title of the feed. sortid:
 *         this has not been figured out yet. firstitemsec: this has not
 *         been figured out yet.
 */
public static JSONArray getSubscriptionList(String sid) {
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpGet get = new HttpGet(SUB_URL + "/list?output=json");
    final BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        final HttpResponse response = client.execute(get);
        final HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        final InputStream in = respEntity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        final JSONObject obj = new JSONObject(arr);
        final JSONArray array = obj.getJSONArray("subscriptions");

        reader.close();
        client.getConnectionManager().shutdown();

        return array;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:org.neo4j.nlp.examples.sentiment.main.java

private static String executePost(String targetURL, String payload) {
    try {/*from w w w  . ja v  a2  s  .  c  o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(targetURL);

        StringEntity input = new StringEntity(payload);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

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

        StringBuilder output = new StringBuilder();
        while (br.read() != -1) {
            output.append(br.readLine()).append('\n');
        }

        httpClient.getConnectionManager().shutdown();

        return "{" + output.toString();

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;
}