热门IT资讯网

springboot httpclient

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,post/** * post * */ public String client(String url, MultiValueMap params){ RestT

post

/**     * post     * */    public String client(String url,  MultiValueMap params){        RestTemplate client = new RestTemplate();        HttpHeaders headers = new HttpHeaders();        //  请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);        HttpEntity> requestEntity = new HttpEntity>(params, headers);        //  执行HTTP请求        ResponseEntity 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 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 params= new LinkedMultiValueMap();        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);    }
0