Search in sources :

Example 56 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by AOSPA.

the class AbstractProxyTest method testParamPreferredOverSystemProperty.

private void testParamPreferredOverSystemProperty(ProxyConfig proxyConfig) throws Exception {
    server.enqueue(new MockResponse().setBody("Via request parameter proxy!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet("http://origin.foo/bar");
    proxyConfig.configure(server, client, request);
    HttpResponse response = client.execute(request);
    assertEquals("Via request parameter proxy!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET http://origin.foo/bar HTTP/1.1", recordedRequest.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 57 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by AOSPA.

the class AbstractProxyTest method testConnectToHttps.

public void testConnectToHttps() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
    server.play();
    HttpClient httpClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, server.getPort()));
    HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
    assertEquals("this response comes via HTTPS", contentToString(response));
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 58 with HttpClient

use of org.apache.http.client.HttpClient in project android_frameworks_base by AOSPA.

the class AbstractProxyTest method testRetryWithProxy.

// http://b/5372438
public void testRetryWithProxy() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    HttpGet request = new HttpGet("http://android.com/foo");
    ProxyConfig.REQUEST_PARAMETER.configure(server, httpProxyClient, request);
    try {
        httpProxyClient.execute(request);
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException)

Example 59 with HttpClient

use of org.apache.http.client.HttpClient in project azure-tools-for-java by Microsoft.

the class SparkRestUtil method getEntity.

//    @Nullable
//    public static List<Application> getApplications(@NotNull ClusterDetail clusterDetail) throws HDIException, IOException {
//        HttpEntity entity = getEntity(clusterDetail, "applications");
//        String entityType = entity.getContentType().getValue();
//        if( entityType.equals("application/json")){
//            String json = EntityUtils.toString(entity);
//            List<Application> apps = objectMapper.readValue(json, TypeFactory.defaultInstance().constructType(List.class, Application.class));
//            return apps;
//        }
//        return null;
//    }
public static HttpEntity getEntity(@NotNull IClusterDetail clusterDetail, @NotNull String restUrl) throws HDIException, IOException {
    provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(clusterDetail.getHttpUserName(), clusterDetail.getHttpPassword()));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
    String url = String.format(SPARK_REST_API_ENDPOINT, clusterDetail.getName(), restUrl);
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    int code = response.getStatusLine().getStatusCode();
    if (code == HttpStatus.SC_OK || code == HttpStatus.SC_CREATED) {
        return response.getEntity();
    } else {
        throw new HDIException(response.getStatusLine().getReasonPhrase(), response.getStatusLine().getStatusCode());
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HDIException(com.microsoft.azure.hdinsight.sdk.common.HDIException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 60 with HttpClient

use of org.apache.http.client.HttpClient in project lucene-solr by apache.

the class BlobRepository method fetchBlob.

/**
   *  Package local for unit tests only please do not use elsewhere
   */
ByteBuffer fetchBlob(String key) {
    Replica replica = getSystemCollReplica();
    String url = replica.getStr(BASE_URL_PROP) + "/.system/blob/" + key + "?wt=filestream";
    HttpClient httpClient = coreContainer.getUpdateShardHandler().getHttpClient();
    HttpGet httpGet = new HttpGet(url);
    ByteBuffer b;
    try {
        HttpResponse entity = httpClient.execute(httpGet);
        int statusCode = entity.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "no such blob or version available: " + key);
        }
        b = SimplePostTool.inputStreamToByteArray(entity.getEntity().getContent());
    } catch (Exception e) {
        if (e instanceof SolrException) {
            throw (SolrException) e;
        } else {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "could not load : " + key, e);
        }
    } finally {
        httpGet.releaseConnection();
    }
    return b;
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Replica(org.apache.solr.common.cloud.Replica) ByteBuffer(java.nio.ByteBuffer) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Aggregations

HttpClient (org.apache.http.client.HttpClient)878 HttpResponse (org.apache.http.HttpResponse)548 HttpGet (org.apache.http.client.methods.HttpGet)394 Test (org.junit.Test)273 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)264 IOException (java.io.IOException)258 HttpPost (org.apache.http.client.methods.HttpPost)197 HttpEntity (org.apache.http.HttpEntity)118 URI (java.net.URI)87 InputStream (java.io.InputStream)81 ArrayList (java.util.ArrayList)64 ClientProtocolException (org.apache.http.client.ClientProtocolException)61 InputStreamReader (java.io.InputStreamReader)59 StringEntity (org.apache.http.entity.StringEntity)58 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)56 MockResponse (com.google.mockwebserver.MockResponse)48 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)48 BufferedReader (java.io.BufferedReader)46 URISyntaxException (java.net.URISyntaxException)45