您好,登錄后才能下訂單哦!
post
/**
* post
* */
public String client(String url, MultiValueMap<String, String> params){
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
// 請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
// 執行HTTP請求
ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
}
get
/**
* get
* */
public String clientGET(String url){
RestTemplate client = new RestTemplate();
// 執行HTTP請求
ResponseEntity<String> response = client.getForEntity(url, String.class);
String body = response.getBody();
HttpStatus statusCode = response.getStatusCode();
int statusCodeValue = response.getStatusCodeValue();
HttpHeaders headers = response.getHeaders();
return response.getBody();
}
public void testPost() throws Exception {
//api url地址
String url = "https://api-test.chinazyjr.net/v1/api/JJT/saveBorrow";
//post請求
// 封裝參數,千萬不要替換為Map與HashMap,否則參數無法傳遞
MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
params.add("data", "{\r\n" +
" \"requestUuid\":\"5918531018\",\r\n" +
" \"platform\":\"JJT\",\r\n" +
" \"requestInfo\":{\r\n" +
" \"userId\":\"3d235a90b51f4b7d\",\r\n" +
" \"contractNo\":\"5918531018\",\r\n" +
" \"bidName\":\"testyyq散8標629\",\r\n" +
" \"retUrl\":null,\r\n" +
" \"retType\":\"1\",\r\n" +
" \"retAmt\":\"10125.28\",\r\n" +
" \"borrTotAmt\":\"10000.00\",\r\n" +
" \"yearRate\":\"6\",\r\n" +
" \"borrowReturnPlan\":[\r\n" +
" {\r\n" +
" \"periodNo\":\"1\",\r\n" +
" \"billDate\":\"2018-04-01\",\r\n" +
" \"shouldAmount\":\"2481.32\",\r\n" +
" \"shouldInterest\":\"50.00\",\r\n" +
" \"shouldServiceFee\":\"0\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"periodNo\":\"2\",\r\n" +
" \"billDate\":\"2018-05-01\",\r\n" +
" \"shouldAmount\":\"2493.73\",\r\n" +
" \"shouldInterest\":\"37.59\",\r\n" +
" \"shouldServiceFee\":\"0\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"periodNo\":\"3\",\r\n" +
" \"billDate\":\"2018-06-01\",\r\n" +
" \"shouldAmount\":\"2506.20\",\r\n" +
" \"shouldInterest\":\"25.12\",\r\n" +
" \"shouldServiceFee\":\"0\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"periodNo\":\"4\",\r\n" +
" \"billDate\":\"2018-07-01\",\r\n" +
" \"shouldAmount\":\"2518.75\",\r\n" +
" \"shouldInterest\":\"12.57\",\r\n" +
" \"shouldServiceFee\":\"0\"\r\n" +
" }\r\n" +
" ],\r\n" +
" \"borrowUserInfo\":{\r\n" +
" \"userName\":\"張玉言\",\r\n" +
" \"sex\":1,\r\n" +
" \"age\":33,\r\n" +
" \"education\":\"哈佛\",\r\n" +
" \"maritalStatus\":\"未婚\",\r\n" +
" \"censusRegisterAddress\":\"江蘇徐州市泉山區sdf\",\r\n" +
" \"childrenFlag\":1,\r\n" +
" \"unitIndustry\":null,\r\n" +
" \"livingAddress\":null,\r\n" +
" \"position\":null,\r\n" +
" \"houseSituation\":null,\r\n" +
" \"houseLoan\":null,\r\n" +
" \"companyNature\":null,\r\n" +
" \"entryDate\":null,\r\n" +
" \"monthlyIncome\":null,\r\n" +
" \"workYears\":null,\r\n" +
" \"houseAssets\":null,\r\n" +
" \"carAssets\":null,\r\n" +
" \"annualEarnings\":null,\r\n" +
" \"valuation\":null,\r\n" +
" \"idCard\":\"532927199308065798\",\r\n" +
" \"mobile\":\"18212000000\"\r\n" +
" },\r\n" +
" \"periodsUnit\":\"3\",\r\n" +
" \"periods\":\"4\"\r\n" +
" }\r\n" +
"}");
//發送http請求并返回結果
String returnStrnig = client(url,params);
System.out.println(returnStrnig);
}
public void testGet() throws Exception {
//api url地址
String url = "https://boss-test.chinazyjr.net/login.html";
String body = clientGET(url);
System.out.println(body);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。