post请求 form-data类型请求数据 --springboot restTemplate
Java——RestTemplate发送POST请求之formData形式
import com.alibaba.fastjson.JSON; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.*; /** * RestTemplate发送POST请求之formData形式 * @return */ public static Map<String, String> formData(){ String url = "http://192.168.1.85:8089/getToken"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); //接口参数 map.add("username","xiaoming"); map.add("password","123456"); //头部类型 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //构造实体对象 HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers); //发起请求,服务地址,请求参数,返回消息体的数据类型 ResponseEntity<String> response = restTemplate.postForEntity(url, param, String.class); //body String body = response.getBody(); System.out.println(body); //JSON格式转为Map类型 Map result = JSON.parseObject(body, Map.class); System.out.println(result); return result; }

参考:
HttpURLConnection post请求“Content-Type“, “multipart/form-data“方式进行请求操作
JAVA HttpURLConnection 发送post请求,数据格式为form-data,form-data传文件,传日志

浙公网安备 33010602011771号