• java httpclient简单使用


    httpClient 官网

    https://hc.apache.org/index.html

    https://hc.apache.org/httpcomponents-client-5.1.x/index.html#

    这里以httpClient5.1为列

    httpClient5.1基本使用

    进入后,可以看到常用demo的一些源代码:

    package org.apache.hc.client5.http.examples;
    
    import java.io.IOException;
    
    import org.apache.hc.client5.http.ClientProtocolException;
    import org.apache.hc.client5.http.classic.methods.HttpGet;
    import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
    import org.apache.hc.client5.http.impl.classic.HttpClients;
    import org.apache.hc.core5.http.ClassicHttpResponse;
    import org.apache.hc.core5.http.HttpEntity;
    import org.apache.hc.core5.http.HttpStatus;
    import org.apache.hc.core5.http.ParseException;
    import org.apache.hc.core5.http.io.HttpClientResponseHandler;
    import org.apache.hc.core5.http.io.entity.EntityUtils;
    
    /**
     * This example demonstrates the use of the {@link HttpClientResponseHandler} to simplify
     * the process of processing the HTTP response and releasing associated resources.
     */
    public class ClientWithResponseHandler {
    
        public static void main(final String[] args) throws Exception {
            try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
                final HttpGet httpget = new HttpGet("http://httpbin.org/get");
    
                System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
    
                // Create a custom response handler
                final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() {
    
                    @Override
                    public String handleResponse(
                            final ClassicHttpResponse response) throws IOException {
                        final int status = response.getCode();
                        if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
                            final HttpEntity entity = response.getEntity();
                            try {
                                return entity != null ? EntityUtils.toString(entity) : null;
                            } catch (final ParseException ex) {
                                throw new ClientProtocolException(ex);
                            }
                        } else {
                            throw new ClientProtocolException("Unexpected response status: " + status);
                        }
                    }
    
                };
                final String responseBody = httpclient.execute(httpget, responseHandler);
                System.out.println("----------------------------------------");
                System.out.println(responseBody);
            }
        }
    
    }

    httpclient5.1 使用maven依赖

     进入页面,即可查看:

  • 相关阅读:
    经典排序算法之直接选择排序
    经典排序算法之归并排序
    经典排序算法之希尔排序
    MAC自带的SVN进行升级
    磁盘随机读写与顺序读写性能对比
    事务锁与脏读、不可重复读、幻读
    处理vue页面406问题纪要
    url-pattern / 与/* 的区别
    打印 request 请求中的参数
    [转] Slf4j MDC机制
  • 原文地址:https://www.cnblogs.com/liyuanhong/p/16007750.html
Copyright © 2020-2023  润新知