springboot httpclient
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot httpclient相关的知识,希望对你有一定的参考价值。
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", "{
" +
" "requestUuid":"5918531018",
" +
" "platform":"JJT",
" +
" "requestInfo":{
" +
" "userId":"3d235a90b51f4b7d",
" +
" "contractNo":"5918531018",
" +
" "bidName":"testyyq散8标629",
" +
" "retUrl":null,
" +
" "retType":"1",
" +
" "retAmt":"10125.28",
" +
" "borrTotAmt":"10000.00",
" +
" "yearRate":"6",
" +
" "borrowReturnPlan":[
" +
" {
" +
" "periodNo":"1",
" +
" "billDate":"2018-04-01",
" +
" "shouldAmount":"2481.32",
" +
" "shouldInterest":"50.00",
" +
" "shouldServiceFee":"0"
" +
" },
" +
" {
" +
" "periodNo":"2",
" +
" "billDate":"2018-05-01",
" +
" "shouldAmount":"2493.73",
" +
" "shouldInterest":"37.59",
" +
" "shouldServiceFee":"0"
" +
" },
" +
" {
" +
" "periodNo":"3",
" +
" "billDate":"2018-06-01",
" +
" "shouldAmount":"2506.20",
" +
" "shouldInterest":"25.12",
" +
" "shouldServiceFee":"0"
" +
" },
" +
" {
" +
" "periodNo":"4",
" +
" "billDate":"2018-07-01",
" +
" "shouldAmount":"2518.75",
" +
" "shouldInterest":"12.57",
" +
" "shouldServiceFee":"0"
" +
" }
" +
" ],
" +
" "borrowUserInfo":{
" +
" "userName":"张玉言",
" +
" "sex":1,
" +
" "age":33,
" +
" "education":"哈佛",
" +
" "maritalStatus":"未婚",
" +
" "censusRegisterAddress":"江苏徐州市泉山区sdf",
" +
" "childrenFlag":1,
" +
" "unitIndustry":null,
" +
" "livingAddress":null,
" +
" "position":null,
" +
" "houseSituation":null,
" +
" "houseLoan":null,
" +
" "companyNature":null,
" +
" "entryDate":null,
" +
" "monthlyIncome":null,
" +
" "workYears":null,
" +
" "houseAssets":null,
" +
" "carAssets":null,
" +
" "annualEarnings":null,
" +
" "valuation":null,
" +
" "idCard":"532927199308065798",
" +
" "mobile":"18212000000"
" +
" },
" +
" "periodsUnit":"3",
" +
" "periods":"4"
" +
" }
" +
"}");
//发送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);
}
以上是关于springboot httpclient的主要内容,如果未能解决你的问题,请参考以下文章