java用HttpPost发送post请求,传body

本文介绍了一种使用Java和HttpClient进行HTTP文件上传的方法,并通过定时任务触发文章生成流程。详细展示了如何设置请求配置、构建请求实体以及处理响应。同时,讨论了异常处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void uploadFileOrg(NowDateDto nowDateDto){
    CloseableHttpClient httpclient = HttpClients.createDefault();
    long stateTime = System.currentTimeMillis();
    log.info("开始调用article定时任务生成文章,当前系统时间==={}",stateTime);
    try {
        HttpPost httppost = new HttpPost(clockAuditArticle);

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000)
                .setSocketTimeout(200000).build();
        httppost.setConfig(requestConfig);
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("nowDate",nowDateDto.getNowDate());
        httppost.setEntity(new StringEntity(JSONObject.toJSONString(paramMap), ContentType.create("application/json", "utf-8")));

        httppost.setHeader("Authorization","aggregete");
        httppost.setHeader("Content-Type", "application/json");

        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                log.info("调用article定时任务生成文章成功,用时====={}",System.currentTimeMillis()-stateTime);
                String responseEntityStr = EntityUtils.toString(response.getEntity());
                System.out.println("--上传返回信息--"+responseEntityStr);
            }else{
                throw new AppBusinessException(UserErrorCode.UPLOAD_ERROR);
            }
            EntityUtils.consume(resEntity);
        } finally {
            response.close();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值