java发送post请求(httpclient)

//发送请求的URL
        String url = "http://localhost:831/ruoyi/ecr/productinfo/findByFldOrderCode";
        //编码格式
        String charset = "UTF-8";
        //请求内容
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fldOrderCode","2022-JM-Q104");
        jsonObject.put("fldReUnid","string");
        jsonObject.put("pageNum",1);
        jsonObject.put("pageSize",10);
        String content = jsonObject.toJSONString();
        //使用HttpClientBuilder创建CloseableHttpClient对象并设置代理.
        HttpHost proxy = new HttpHost("127.0.0.1",8888);
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
        //HTTP请求类型创建HttpPost实例
        HttpPost post = new HttpPost(url);
        //使用addHeader方法添加请求头部,诸如User-Agent, Accept-Encoding等参数.
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        // 组织数据
        StringEntity se = null;
        try {
        	//设置编码格式
            se = new StringEntity(content,charset);
            //设置数据类型
            se.setContentType("application/json");
            //对于POST请求,把请求体填充进HttpPost实体.
            post.setEntity(se);
            //通过执行HttpPost请求获取CloseableHttpResponse实例 ,从此CloseableHttpResponse实例中获取状态码,错误信息,以及响应页面等等.
            CloseableHttpResponse response = client.execute(post);
            //通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理
            HttpEntity entity = response.getEntity();
            String resData = EntityUtils.toString(response.getEntity());
            System.out.println(resData);
            //最后关闭HttpClient资源.
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//RestTemplate上传附件
public ResultData openFormUpload(@RequestParam Map<String, Object> params,
                                     @RequestParam("file") MultipartFile file) throws IOException {
    RestTemplate restClient = new RestTemplate();
    String uploadUrl = formDomain;
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    param.add("instanceId", params.get("instanceId"));
    param.add("sectionId", params.get("sectionId"));
    param.add("loginName", params.get("loginName"));
    param.add("langType", params.get("langType"));
    param.add("isAboard", params.get("isAboard"));

    String fileName = file.getOriginalFilename();
    //临时文件存放路径
    String filePath = this.getClass().getResource("/").getPath() + fileName;
    File tempFile = new File(filePath);
    file.transferTo(tempFile);
    FileSystemResource resource = new FileSystemResource(tempFile);
    param.add("file", resource);
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("multipart/form-data;charset=UTF-8");
    headers.setContentType(type);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(param, headers);
    String result = null;
    try {
        result = restClient.postForObject(uploadUrl, requestEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject data = jsonObject.getObject("data", JSONObject.class);
        return ResultDataUtil.buildSuccess("200","操作成功!",data);
    } catch (RestClientException e) {
        return ResultDataUtil.buildError("201","调用接口失败!",e.getMessage());
    } finally {
        tempFile.delete();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值