Search in sources :

Example 36 with HttpClient

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

the class CookiesTest method testCookiesWithNonMatchingCase.

/**
     * Test that cookies aren't case-sensitive with respect to hostname.
     * http://b/3167208
     */
public void testCookiesWithNonMatchingCase() throws Exception {
    // use a proxy so we can manipulate the origin server's host name
    server = new MockWebServer();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=first; Domain=my.t-mobile.com").addHeader("Set-Cookie: b=second; Domain=.T-mobile.com").addHeader("Set-Cookie: c=third; Domain=.t-mobile.com").setBody("This response sets some cookies."));
    server.enqueue(new MockResponse().setBody("This response gets those cookies back."));
    server.play();
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", server.getPort()));
    HttpResponse getCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    getCookies.getEntity().consumeContent();
    server.takeRequest();
    HttpResponse sendCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    sendCookies.getEntity().consumeContent();
    RecordedRequest sendCookiesRequest = server.takeRequest();
    assertContains(sendCookiesRequest.getHeaders(), "Cookie: a=first; b=second; c=third");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 37 with HttpClient

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

the class AbstractProxyTest method testConnectViaProxy.

/**
     * http://code.google.com/p/android/issues/detail?id=2690
     */
private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setBody("this response comes via a proxy");
    server.enqueue(mockResponse);
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    HttpGet request = new HttpGet("http://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a proxy", contentToString(response));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET http://android.com/foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
}
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 38 with HttpClient

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

the class AbstractProxyTest method testExplicitNoProxyCancelsSystemProperty.

public void testExplicitNoProxyCancelsSystemProperty() throws Exception {
    server.enqueue(new MockResponse().setBody("Via the origin server!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet(server.getUrl("/bar").toURI());
    request.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, ConnRouteParams.NO_HOST);
    HttpResponse response = client.execute(request);
    assertEquals("Via the origin server!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET /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 39 with HttpClient

use of org.apache.http.client.HttpClient in project opennms by OpenNMS.

the class DiscoveryIT method canDiscoverRemoteNodes.

@Test
public void canDiscoverRemoteNodes() throws ClientProtocolException, IOException {
    Date startOfTest = new Date();
    final String tomcatIp = minionSystem.getContainerInfo(ContainerAlias.TOMCAT).networkSettings().ipAddress();
    final InetSocketAddress opennmsHttp = minionSystem.getServiceAddress(ContainerAlias.OPENNMS, 8980);
    final HttpHost opennmsHttpHost = new HttpHost(opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort());
    HttpClient instance = HttpClientBuilder.create().setRedirectStrategy(// Ignore the 302 response to the POST
    new LaxRedirectStrategy()).build();
    Executor executor = Executor.newInstance(instance).auth(opennmsHttpHost, "admin", "admin").authPreemptive(opennmsHttpHost);
    // Configure Discovery with the specific address of our Tomcat server
    // No REST endpoint is currently available to configure the Discovery daemon
    // so we resort to POSTin nasty form data
    executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=AddSpecific", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("specificipaddress", tomcatIp).add("specifictimeout", "2000").add("specificretries", "1").add("initialsleeptime", "30000").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
    executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=SaveAndRestart", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("initialsleeptime", "1").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
    InetSocketAddress pgsql = minionSystem.getServiceAddress(ContainerAlias.POSTGRES, 5432);
    HibernateDaoFactory daoFactory = new HibernateDaoFactory(pgsql);
    EventDao eventDao = daoFactory.getDao(EventDaoHibernate.class);
    Criteria criteria = new CriteriaBuilder(OnmsEvent.class).eq("eventUei", EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI).ge("eventTime", startOfTest).toCriteria();
    await().atMost(1, MINUTES).pollInterval(10, SECONDS).until(DaoUtils.countMatchingCallable(eventDao, criteria), greaterThan(0));
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) HibernateDaoFactory(org.opennms.smoketest.utils.HibernateDaoFactory) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Executor(org.apache.http.client.fluent.Executor) EventDao(org.opennms.netmgt.dao.api.EventDao) InetSocketAddress(java.net.InetSocketAddress) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) Criteria(org.opennms.core.criteria.Criteria) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) Date(java.util.Date) Test(org.junit.Test)

Example 40 with HttpClient

use of org.apache.http.client.HttpClient in project tdi-studio-se by Talend.

the class WsdlTokenManager method getSOAPResponse.

private static String getSOAPResponse(URI issuerUri, String soapEnvelope) {
    HttpResponse response = null;
    // Create the request that will submit the request to the server
    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 180000);
        HttpClient client = new SystemDefaultHttpClient(params);
        HttpPost post = new HttpPost(issuerUri);
        StringEntity entity = new StringEntity(soapEnvelope);
        post.setHeader("Content-Type", "application/soap+xml; charset=UTF-8");
        post.setEntity(entity);
        response = client.execute(post);
        return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
    return null;
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpClient(org.apache.http.client.HttpClient) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

HttpClient (org.apache.http.client.HttpClient)941 HttpResponse (org.apache.http.HttpResponse)584 HttpGet (org.apache.http.client.methods.HttpGet)429 IOException (java.io.IOException)308 Test (org.junit.Test)275 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)272 HttpPost (org.apache.http.client.methods.HttpPost)212 HttpEntity (org.apache.http.HttpEntity)128 URI (java.net.URI)92 InputStream (java.io.InputStream)81 StringEntity (org.apache.http.entity.StringEntity)74 ArrayList (java.util.ArrayList)69 ClientProtocolException (org.apache.http.client.ClientProtocolException)66 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)61 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)61 InputStreamReader (java.io.InputStreamReader)59 URL (java.net.URL)57 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)53 URISyntaxException (java.net.URISyntaxException)50 MockResponse (com.google.mockwebserver.MockResponse)48