java.lang.IllegalStateException:

2022-08-01  本文已影响0人  媛猿YY

最近项目中压测,利用httpclient访问接口报错。错误信息如下:


image.png

改用线程池中访问https\http链接类型接口完整代码,使用方法:

public class HttpClientUtils {
    private static CloseableHttpClient HTTPCLIENT;
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
    private static RequestConfig REQUESTCONFIG = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setConnectTimeout(1000)
            .setConnectionRequestTimeout(1000)
            .build();
    static PoolingHttpClientConnectionManager cm;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        HTTPCLIENT = HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
            return false;
        }).setDefaultRequestConfig(REQUESTCONFIG).setConnectionManager(cm).build();

    }


    public static String get(String url) throws IOException {
        try {

            HttpGet httpget = new HttpGet(url);
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        logger.error("请求{},出现了错误{}", url, status);
                        return "出错了";
                        //throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = HTTPCLIENT.execute(httpget, responseHandler);
            return responseBody;
        } finally {

        }
    }

    public static String post(String url,Map map) throws IOException {
        String result = null;
        HttpPost post = new HttpPost(url);
//        System.out.println(map);
        post.setHeader("content-type","application/json");
        String param = JSON.toJSONString(map);
        StringEntity entity = new StringEntity(param,"UTF-8");
        post.setEntity(entity);
        try {
            HttpResponse response = HTTPCLIENT.execute(post);
            result = EntityUtils.toString(response.getEntity(),"UTF-8");
            // System.out.println(result);
           return result;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";

    }


    public static HttpResponse postResponse(String url,Map map) throws IOException {
        String result = null;
        HttpPost post = new HttpPost(url);
//        System.out.println(map);
        post.setHeader("content-type","application/json");
        String param = JSON.toJSONString(map);
        StringEntity entity = new StringEntity(param,"UTF-8");
        post.setEntity(entity);
        try {
            HttpResponse response = HTTPCLIENT.execute(post);
            System.out.println(response.getAllHeaders());
            System.out.println(response.getEntity());
            result = EntityUtils.toString(response.getEntity(),"UTF-8");
            // System.out.println(result);
            return response;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}
HttpPost post = new HttpPost(url);
String result = HttpClientUtils.post(url, map);
上一篇下一篇

猜你喜欢

热点阅读