Search in sources :

Example 61 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project android_frameworks_base by ParanoidAndroid.

the class FsUtils method getHttpClient.

private static HttpClient getHttpClient() {
    if (sHttpClient == null) {
        HttpParams params = new BasicHttpParams();
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), ForwarderManager.HTTP_PORT));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), ForwarderManager.HTTPS_PORT));
        ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
        sHttpClient = new DefaultHttpClient(connectionManager, params);
        HttpConnectionParams.setSoTimeout(sHttpClient.getParams(), HTTP_TIMEOUT_MS);
        HttpConnectionParams.setConnectionTimeout(sHttpClient.getParams(), HTTP_TIMEOUT_MS);
    }
    return sHttpClient;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 62 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project Anki-Android by Ramblurr.

the class HttpFetcher method fetchThroughHttp.

public static String fetchThroughHttp(String address, String encoding) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(address);
        HttpResponse response = httpClient.execute(httpGet, localContext);
        if (!response.getStatusLine().toString().contains("OK")) {
            return "FAILED";
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(encoding)));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        return stringBuilder.toString();
    } catch (Exception e) {
        return "FAILED with exception: " + e.getMessage();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) BufferedReader(java.io.BufferedReader) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 63 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project Anki-Android by Ramblurr.

the class HttpUtility method postReport.

public static Boolean postReport(String url, List<NameValuePair> values) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(values));
        HttpResponse response = httpClient.execute(httpPost);
        switch(response.getStatusLine().getStatusCode()) {
            case 200:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s", url));
                return true;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s message", url));
                Log.e(AnkiDroidApp.TAG, String.format("%d: %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 64 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project android_frameworks_base by ParanoidAndroid.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 65 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project android_frameworks_base by ParanoidAndroid.

the class DefaultHttpClientTest method testServerClosesOutput.

private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
    server.enqueue(new MockResponse().setBody("This connection won't pool properly").setSocketPolicy(socketPolicy));
    server.enqueue(new MockResponse().setBody("This comes after a busted connection"));
    server.play();
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse a = client.execute(new HttpGet(server.getUrl("/a").toURI()));
    assertEquals("This connection won't pool properly", contentToString(a));
    assertEquals(0, server.takeRequest().getSequenceNumber());
    HttpResponse b = client.execute(new HttpGet(server.getUrl("/b").toURI()));
    assertEquals("This comes after a busted connection", contentToString(b));
    // sequence number 0 means the HTTP socket connection was not reused
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)342 HttpResponse (org.apache.http.HttpResponse)199 HttpGet (org.apache.http.client.methods.HttpGet)167 HttpClient (org.apache.http.client.HttpClient)139 IOException (java.io.IOException)104 Test (org.junit.Test)65 HttpPost (org.apache.http.client.methods.HttpPost)64 HttpEntity (org.apache.http.HttpEntity)55 BasicHttpParams (org.apache.http.params.BasicHttpParams)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)48 InputStream (java.io.InputStream)47 HttpParams (org.apache.http.params.HttpParams)43 Scheme (org.apache.http.conn.scheme.Scheme)39 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)38 ArrayList (java.util.ArrayList)32 URI (java.net.URI)30 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)30 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)29 InputStreamReader (java.io.InputStreamReader)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)27