Search in sources :

Example 11 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project musicbrainz-android by jdamcd.

the class MusicBrainzWebClient method autenticateCredentials.

@Override
public boolean autenticateCredentials() throws IOException {
    HttpGet authenticationTest = new HttpGet(QueryBuilder.authenticationCheck());
    authenticationTest.setHeader("Accept", "application/xml");
    try {
        httpClient.execute(authenticationTest, new BasicResponseHandler());
    } catch (HttpResponseException e) {
        return false;
    }
    return true;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 12 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project Klyph by jonathangerbaud.

the class HttpRequest2 method send.

public String send() {
    client = AndroidHttpClient.newInstance("Android");
    HttpPost request = new HttpPost(url);
    if (params != null) {
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
            entity.setContentEncoding(HTTP.UTF_8);
            request.setEntity(entity);
        } catch (UnsupportedEncodingException e) {
            Log.e("", "UnsupportedEncodingException: " + e);
        }
    }
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = "";
    try {
        response = client.execute(request, responseHandler);
    } catch (ClientProtocolException e) {
        Log.e("HttpRequest2", e.toString());
    } catch (IOException e) {
        Log.e("HttpRequest2", e.toString());
    }
    if (LOG_RESPONSE == true)
        Log.i("HttpRequest2", response);
    if (HTML_TRANSFORM == true)
        response = Html.fromHtml(response).toString();
    close();
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project pinpoint by naver.

the class HttpClientIT method test.

@Test
public void test() throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost("http://www.naver.com");
        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        httpClient.execute(post, responseHandler);
    } catch (Exception ignored) {
    } finally {
        if (null != httpClient && null != httpClient.getConnectionManager()) {
            httpClient.getConnectionManager().shutdown();
        }
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    Class<?> connectorClass;
    try {
        connectorClass = Class.forName("org.apache.http.impl.conn.ManagedClientConnectionImpl");
    } catch (ClassNotFoundException e) {
        connectorClass = Class.forName("org.apache.http.impl.conn.AbstractPooledConnAdapter");
    }
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", AbstractHttpClient.class.getMethod("execute", HttpUriRequest.class, ResponseHandler.class)));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", connectorClass.getMethod("open", HttpRoute.class, HttpContext.class, HttpParams.class), annotation("http.internal.display", "www.naver.com")));
    verifier.verifyTrace(event("HTTP_CLIENT_4", HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class, HttpContext.class), null, null, "www.naver.com", annotation("http.url", "/"), annotation("http.status.code", 200), annotation("http.io", anyAnnotationValue())));
    verifier.verifyTraceCount(0);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 14 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method shouldSupportBasicResponseHandlerHandleResponse.

@Test
public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception {
    FakeHttp.addPendingHttpResponse(200, "OK", new BasicHeader("Content-Type", "text/plain"));
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet("http://www.nowhere.org"));
    assertThat(((HttpUriRequest) FakeHttp.getSentHttpRequest(0)).getURI()).isEqualTo(URI.create("http://www.nowhere.org"));
    Assert.assertNotNull(response);
    String responseStr = new BasicResponseHandler().handleResponse(response);
    Assert.assertEquals("OK", responseStr);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponse(org.apache.http.HttpResponse) BasicHeader(org.apache.http.message.BasicHeader) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 15 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project apn-proxy by apn-proxy.

the class TestProxyWithHttpClient method test.

private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(2000);
    cm.setDefaultMaxPerRoute(40);
    cm.setDefaultConnectionConfig(connectionConfig);
    CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm).disableContentCompression().disableCookieManagement().build();
    HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());
    RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true).setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpGet request = new HttpGet(uri);
    request.setConfig(config);
    try {
        CloseableHttpResponse httpResponse = httpClient.execute(request);
        Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
        if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
            Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
        }
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseHandler.handleResponse(httpResponse);
        httpResponse.close();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) ConnectionConfig(org.apache.http.config.ConnectionConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)39 HttpGet (org.apache.http.client.methods.HttpGet)26 Test (org.junit.Test)15 IOException (java.io.IOException)10 HttpPost (org.apache.http.client.methods.HttpPost)10 ArrayList (java.util.ArrayList)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 HttpClient (org.apache.http.client.HttpClient)6 HttpResponseException (org.apache.http.client.HttpResponseException)6 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2