47 lines
1.2 KiB
Java
47 lines
1.2 KiB
Java
package pmml;
|
|
|
|
|
|
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;
|
|
|
|
public class RestTemplateTest {
|
|
|
|
public static void main(String[] args)throws Exception{
|
|
|
|
RestTemplate rt = new RestTemplate();
|
|
|
|
MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
|
|
map.add("name", "tiger");
|
|
|
|
// 设置请求头
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
// 创建HTTP实体
|
|
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
|
|
|
|
// 发送POST请求
|
|
String url = "http://47.99.93.74:8090/mockapi/hello2";
|
|
String response = rt.postForObject(url, request, String.class);
|
|
|
|
System.out.println(response);
|
|
|
|
|
|
ResponseEntity<String> responseEntity = rt.postForEntity(url, request, String.class);
|
|
System.out.println(responseEntity.getBody());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|