import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public static void main(String args[]){
String url="http://127.0.0.1/api/getToken";
HttpHeaders header = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RestTemplate restTemplate=new RestTemplate();
MultiValueMap<String, String> forms= new LinkedMultiValueMap<String, String>();
forms.put("username", Collections.singletonList("username"));
forms.put("password", Collections.singletonList("password"));
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<MultiValueMap<String, String>>(forms, header);
String result = restTemplate.postForObject(url, httpEntity, String.class);
}
Java后端发送请求Content-Type:application/x-www-form-urlencoded
最新推荐文章于 2024-07-26 12:28:02 发布