Search in sources :

Example 66 with HttpGet

use of org.apache.http.client.methods.HttpGet in project openkit-android by OpenKit.

the class OKHTTPClient method get.

public static void get(String relativeUrl, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    HttpGet request = new HttpGet(AsyncHttpClient.getUrlWithQueryString(getAbsoluteUrl(relativeUrl), params));
    sign(request);
    client.get(request, responseHandler);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet)

Example 67 with HttpGet

use of org.apache.http.client.methods.HttpGet in project openkit-android by OpenKit.

the class AsyncHttpClient method get.

/**
     * Perform a HTTP GET request and track the Android Context which initiated
     * the request with customized headers
     * 
     * @param url the URL to send the request to.
     * @param headers set headers only for this request
     * @param params additional GET parameters to send with the request.
     * @param responseHandler the response handler instance that should handle
     *        the response.
     */
public void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, null, responseHandler, context);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet)

Example 68 with HttpGet

use of org.apache.http.client.methods.HttpGet in project DataX by alibaba.

the class RetryUtilTest method testRetryAsync3.

//@Test
@Ignore
public void testRetryAsync3() throws Exception {
    final int TIME_OUT = 30000;
    ThreadPoolExecutor executor = RetryUtil.createThreadPoolExecutor();
    String res = RetryUtil.asyncExecuteWithRetry(new Callable<String>() {

        @Override
        public String call() throws Exception {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).setConnectionRequestTimeout(TIME_OUT).setStaleConnectionCheckEnabled(true).build();
            HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(10).setMaxConnPerRoute(10).setDefaultRequestConfig(requestConfig).build();
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(new URI("http://0.0.0.0:8080/test"));
            httpClient.execute(httpGet);
            return OK;
        }
    }, 3, 1000L, false, 6000L, executor);
    Assert.assertEquals(res, OK);
//        Assert.assertEquals(RetryUtil.EXECUTOR.getActiveCount(), 0);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException)

Example 69 with HttpGet

use of org.apache.http.client.methods.HttpGet in project DataX by alibaba.

the class HttpClientUtilTest method testExecuteAndGetWithRetry2.

/**
     * 和测试方法一样:testExecuteAndGetWithRetry(),只是换了一种写法,直接采用 Mockito 进行验证的。
     */
@Test
public void testExecuteAndGetWithRetry2() throws Exception {
    String url = "http://127.0.0.1/:8080";
    HttpRequestBase httpRequestBase = new HttpGet(url);
    HttpClientUtil httpClientUtil = Mockito.spy(HttpClientUtil.getHttpClientUtil());
    Mockito.doThrow(new Exception("one")).doThrow(new Exception("two")).doThrow(new Exception("three")).doReturn("成功").when(httpClientUtil).executeAndGet(httpRequestBase);
    String str = httpClientUtil.executeAndGetWithRetry(httpRequestBase, 4, 1000l);
    Assert.assertEquals(str, "成功");
    Mockito.reset(httpClientUtil);
    Mockito.doThrow(new Exception("one")).doThrow(new Exception("two")).doThrow(new Exception("three")).doReturn("成功").when(httpClientUtil).executeAndGet(httpRequestBase);
    try {
        httpClientUtil.executeAndGetWithRetry(httpRequestBase, 2, 1000l);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof DataXException);
        Assert.assertTrue(e.getMessage().contains("two"));
    }
    httpClientUtil.destroy();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) DataXException(com.alibaba.datax.common.exception.DataXException) URISyntaxException(java.net.URISyntaxException) DataXException(com.alibaba.datax.common.exception.DataXException) Test(org.junit.Test)

Example 70 with HttpGet

use of org.apache.http.client.methods.HttpGet in project DataX by alibaba.

the class HttpClientUtilTest method testExecuteAndGetWithRetry.

@Test
public void testExecuteAndGetWithRetry() throws Exception {
    String url = "http://127.0.0.1/:8080";
    HttpRequestBase httpRequestBase = new HttpGet(url);
    HttpClientUtil httpClientUtil = PowerMockito.spy(HttpClientUtil.getHttpClientUtil());
    PowerMockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第1次");
            return new Exception("失败第1次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第2次");
            return new Exception("失败第2次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第3次");
            return new Exception("失败第3次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第4次");
            return new Exception("失败第4次");
        }
    }).doReturn("成功").when(httpClientUtil).executeAndGet(any(HttpRequestBase.class));
    String str = httpClientUtil.executeAndGetWithRetry(httpRequestBase, 5, 1000l);
    Assert.assertEquals(str, "成功");
    try {
        httpClientUtil.executeAndGetWithRetry(httpRequestBase, 2, 1000l);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof DataXException);
    }
    httpClientUtil.destroy();
}
Also used : Answer(org.mockito.stubbing.Answer) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpGet(org.apache.http.client.methods.HttpGet) DataXException(com.alibaba.datax.common.exception.DataXException) URISyntaxException(java.net.URISyntaxException) DataXException(com.alibaba.datax.common.exception.DataXException) Test(org.junit.Test)

Aggregations

HttpGet (org.apache.http.client.methods.HttpGet)1143 HttpResponse (org.apache.http.HttpResponse)717 Test (org.junit.Test)504 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)242 TestHttpClient (io.undertow.testutils.TestHttpClient)239 IOException (java.io.IOException)200 HttpClient (org.apache.http.client.HttpClient)185 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)179 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)176 HttpEntity (org.apache.http.HttpEntity)152 Header (org.apache.http.Header)133 InputStream (java.io.InputStream)103 URI (java.net.URI)83 JsonNode (com.fasterxml.jackson.databind.JsonNode)68 ArrayList (java.util.ArrayList)60 MockResponse (com.google.mockwebserver.MockResponse)54 HttpPost (org.apache.http.client.methods.HttpPost)54 Deployment (org.activiti.engine.test.Deployment)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)46 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)45