模拟HTTP请求

创建POST请求

        HttpPost post = new HttpPost(url);
        // 创建HttpClient
        CloseableHttpClient client = HttpClients.createDefault();
        UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(new ArrayList(), "UTF-8");  
        post.setEntity(uefEntity);
        CloseableHttpResponse response = client.execute(post);
        // 获取POST请求状态码
        if(response.getStatusLine().getStatusCode() != 200){
            logger.info("http result error:" + EntityUtils.toString(response.getEntity()));
            return null;
        }
        // 获取返回值
        String res = EntityUtils.toString(response.getEntity());
        logger.info("http result:" + res);
        return res;

你可能感兴趣的:(模拟HTTP请求)