赞
踩
1. form-data 格式 : 以 HttpEntity< MultiValueMap<String, Object>>形式传过去
HttpHeaders headers = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
// 设置以表单的方式提交
headers.add("Content-Type",MediaType.MULTIPART_FORM_DATA_VALUE);
headers.add("token","aaaaaa");
//将请求头部和参数合成一个请求
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("status","aaa");
paramMap.add("compere","aa");
paramMap.add("meetingSummary",ccpcGroupZzsh.getMeetingSummary());
paramMap.add("token",bhToken);
HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers);
//执行HTTP请求
String url="http://"+host+"/bhccpc/zzsh/newCcpcGroupZzsh";
ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class );
json格式: 以 HttpEntity json字符串形式传过去
HttpHeaders headers = new HttpHeaders(); HttpMethod method = HttpMethod.POST; // 设置以json的方式提交 headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE); headers.add("token",loginUser.getBhToken()); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //将请求头部和参数合成一个请求 Map paramMap = new HashMap<>(); paramMap.put("page",String.valueOf(pageParams.getCurrent())); paramMap.put("rows",String.valueOf(pageParams.getSize())); paramMap.put("userId",String.valueOf(memberId)); JSONObject jsonObj = new JSONObject(paramMap); HttpEntity<String> requestEntity = new HttpEntity<>(jsonObj.toString(), headers); //执行HTTP请求,将返回的结构使用ResultVO类格式化 String url="http://"+host+"/bhccpc/partyfee/detailMemberByPage"; String response = restTemplate.postForObject(url, requestEntity,String.class );
文件参数:
//获取登录人 CurrentUser loginUser = loginUserService.findLoginUser(); String token = loginUser.getBhToken(); Long userId = loginUser.getUserId(); BizAssert.isTrue(loginUser != null, "请先登录!"); String currentTimeStr = String.valueOf(System.currentTimeMillis()); //组织生活会 HttpHeaders headers = new HttpHeaders(); HttpMethod method = HttpMethod.POST; // 以表单的方式提交 headers.add("Content-Type",MediaType.MULTIPART_FORM_DATA_VALUE); headers.add("token", token); //将请求头部和参数合成一个请求 MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>(); try { byte[] bytes = file.getBytes(); ByteArrayResource fileAsResource = new ByteArrayResource(bytes) { @Override public String getFilename() { return file.getOriginalFilename(); } @Override public long contentLength() { return file.getSize(); } }; paramMap.add("file",fileAsResource); } catch (IOException e) { e.printStackTrace(); throw new BizException("上传失败"); } paramMap.add("token",token); HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers); //执行HTTP请求,将返回的结构使用ResultVO类格式化 String url="http://"+host+"/bhccpc/file/savefile"; ResponseEntity<String> result = restTemplate.exchange(url, method, requestEntity,String.class ); return R.success(result);
public void aaa() { url1="http://127.0.0.1:8080/getcityqoe" ; Map<String, String> headerMap = new HashMap<String, String>(); headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); //参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("begintime", "2023-12-06 15:10")); params.add(new BasicNameValuePair("endtime", "2023-12-06 16:10")); params.add(new BasicNameValuePair("city","1")); params.add(new BasicNameValuePair("method", "getcityqoe")); RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build(); HttpResponse response=null; HttpClient httpclient = HttpClients.custom().setRetryHandler(myRetryHandler).build(); try { HttpPost post = new HttpPost(url); // 将JSON进行UTF-8编码,以便传输中文 if(!headMap.isEmpty()){ for (Map.Entry<String, String> entry : headMap.entrySet()) { post.setHeader(entry.getKey(), entry.getValue()); } } post.setEntity(new UrlEncodedFormEntity(params , "UTF-8")); post.setConfig(reqConfig); response=httpclient.execute(post); System.out.println(response); return EntityUtils.toString(response.getEntity(),"UTF-8"); } catch (Exception e) { e.printStackTrace(); return null; }finally{ httpclient.getConnectionManager().shutdown(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。