Search in sources :

Example 31 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientExecuteProxy method main.

public static void main(final String[] args) throws Exception {
    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
        final HttpHost target = new HttpHost("https", "httpbin.org", 443);
        final HttpHost proxy = new HttpHost("http", "127.0.0.1", 8080);
        final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        final HttpGet request = new HttpGet("/get");
        request.setConfig(config);
        System.out.println("Executing request " + request.getMethod() + " " + request.getUri() + " via " + proxy);
        try (final CloseableHttpResponse response = httpclient.execute(target, request)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Example 32 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientExecuteSOCKS method main.

public static void main(final String[] args) throws Exception {
    final Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new MyConnectionSocketFactory()).build();
    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
    try (final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
        final InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
        final HttpClientContext context = HttpClientContext.create();
        context.setAttribute("socks.address", socksaddr);
        final HttpHost target = new HttpHost("http", "httpbin.org", 80);
        final HttpGet request = new HttpGet("/get");
        System.out.println("Executing request " + request.getMethod() + " " + request.getUri() + " via SOCKS proxy " + socksaddr);
        try (final CloseableHttpResponse response = httpclient.execute(target, request, context)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) ConnectionSocketFactory(org.apache.hc.client5.http.socket.ConnectionSocketFactory) InetSocketAddress(java.net.InetSocketAddress) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 33 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientFormLogin method main.

public static void main(final String[] args) throws Exception {
    final BasicCookieStore cookieStore = new BasicCookieStore();
    try (final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build()) {
        final HttpGet httpget = new HttpGet("https://someportal/");
        try (final CloseableHttpResponse response1 = httpclient.execute(httpget)) {
            final HttpEntity entity = response1.getEntity();
            System.out.println("Login form get: " + response1.getCode() + " " + response1.getReasonPhrase());
            EntityUtils.consume(entity);
            System.out.println("Initial set of cookies:");
            final List<Cookie> cookies = cookieStore.getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i));
                }
            }
        }
        final ClassicHttpRequest login = ClassicRequestBuilder.post().setUri(new URI("https://someportal/")).addParameter("IDToken1", "username").addParameter("IDToken2", "password").build();
        try (final CloseableHttpResponse response2 = httpclient.execute(login)) {
            final HttpEntity entity = response2.getEntity();
            System.out.println("Login form get: " + response2.getCode() + " " + response2.getReasonPhrase());
            EntityUtils.consume(entity);
            System.out.println("Post logon cookies:");
            final List<Cookie> cookies = cookieStore.getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i));
                }
            }
        }
    }
}
Also used : Cookie(org.apache.hc.client5.http.cookie.Cookie) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCookieStore(org.apache.hc.client5.http.cookie.BasicCookieStore) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) URI(java.net.URI)

Example 34 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientInterceptors method main.

public static void main(final String[] args) throws Exception {
    try (final CloseableHttpClient httpclient = HttpClients.custom().addRequestInterceptorFirst(new HttpRequestInterceptor() {

        private final AtomicLong count = new AtomicLong(0);

        @Override
        public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
            request.setHeader("request-id", Long.toString(count.incrementAndGet()));
        }
    }).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new ExecChainHandler() {

        @Override
        public ClassicHttpResponse execute(final ClassicHttpRequest request, final ExecChain.Scope scope, final ExecChain chain) throws IOException, HttpException {
            final Header idHeader = request.getFirstHeader("request-id");
            if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
                final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
                response.setEntity(new StringEntity("bad luck", ContentType.TEXT_PLAIN));
                return response;
            } else {
                return chain.proceed(request, scope);
            }
        }
    }).build()) {
        for (int i = 0; i < 20; i++) {
            final HttpGet httpget = new HttpGet("http://httpbin.org/get");
            System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
            try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
                System.out.println("----------------------------------------");
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                System.out.println(EntityUtils.toString(response.getEntity()));
            }
        }
    }
}
Also used : ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) IOException(java.io.IOException) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecChainHandler(org.apache.hc.client5.http.classic.ExecChainHandler) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Header(org.apache.hc.core5.http.Header) ExecChain(org.apache.hc.client5.http.classic.ExecChain) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) EntityDetails(org.apache.hc.core5.http.EntityDetails) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 35 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientMultipartFormPost method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("File path not given");
        System.exit(1);
    }
    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
        final HttpPost httppost = new HttpPost("http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample");
        final FileBody bin = new FileBody(new File(args[0]));
        final StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
        final HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();
        httppost.setEntity(reqEntity);
        System.out.println("executing request " + httppost);
        try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
            System.out.println("----------------------------------------");
            System.out.println(response);
            final HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
            }
            EntityUtils.consume(resEntity);
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) FileBody(org.apache.hc.client5.http.entity.mime.FileBody) HttpEntity(org.apache.hc.core5.http.HttpEntity) StringBody(org.apache.hc.client5.http.entity.mime.StringBody) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) File(java.io.File)

Aggregations

HttpHost (org.apache.hc.core5.http.HttpHost)32 HttpRequest (org.apache.hc.core5.http.HttpRequest)25 HttpResponse (org.apache.hc.core5.http.HttpResponse)24 StatusLine (org.apache.hc.core5.http.message.StatusLine)22 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)22 IOException (java.io.IOException)21 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)19 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)19 HttpConnection (org.apache.hc.core5.http.HttpConnection)19 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)17 Header (org.apache.hc.core5.http.Header)17 HttpException (org.apache.hc.core5.http.HttpException)16 CountDownLatch (java.util.concurrent.CountDownLatch)13 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)13 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)12 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)12 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)12 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)12 EntityDetails (org.apache.hc.core5.http.EntityDetails)11 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)11