• spring boot发送HTTP请求


    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    
    
    
        public String doGet(String url){
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> resp = restTemplate.getForEntity(url,String.class);
            return resp.getBody();
        }
    
        //formdata post请求
        public String doPost(String queryUrl, HashMap<String,Object> queryParam) {
            try {
                RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//这里可能根据需要改成application/json方式 
                ObjectMapper om = new ObjectMapper();
                String queryString = om.writeValueAsString(queryParam);
                HttpEntity<String> entity = new HttpEntity<>(queryString,headers);
                ResponseEntity<String> res = restTemplate.postForEntity(queryUrl,entity,String.class);
                return res.getBody();
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return null;
        }

     hutool发送的post请求,好像一定要用它的jsonobject组装参数,不然用不了,不知道什么原因

    cn.hutool.json.JSONObject queryParam = JSONUtil.createObj();
    queryParam.put("data",keys);
    
    //////////////
    public String doPost(String queryUrl, cn.hutool.json.JSONObject queryParam) {
            try {
    
                String result = HttpRequest.post(queryUrl)
                        .header("Content-Type","application/json")
                        .body(queryParam.toString())
                        .execute().body();
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
    }
  • 相关阅读:
    PHP post接口返回数据
    wamp 安装多版本php
    关于WAMP的apache 人多了就访问非常卡的问题解决方法(转)
    在生产环境上重装wamp
    wamp不记录访问日志的办法
    oracle数据库锁表解决办法
    wampserver 中127.0.0.1可以访问,但localhost无法访问
    PLSQL中查询数据的时候查询结果显示中文乱码(转)
    Could not read from remote repository
    17-案例
  • 原文地址:https://www.cnblogs.com/bing2017/p/14658571.html
Copyright © 2020-2023  润新知