• Hutool-http


    • Maven
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.9</version>
    </dependency>
    
    • 笔记
    import cn.hutool.http.Header;
    import cn.hutool.http.HttpRequest;
    import cn.hutool.http.HttpResponse;
    import cn.hutool.http.HttpUtil;
    import com.alibaba.fastjson.JSONObject;
    import java.util.Arrays;
    
    public class Hutool_http {
    
        public static void main(String[] args) {
            //----------------------------  HttpUtil get  ----------------------------
            String result = HttpUtil.get("https://api.doctorxiong.club/v1/fund/position?code=003634");
            System.out.println(JSONObject.parseObject(result));
    
            JSONObject json = new JSONObject();
            json.put("code", "003634");
            String result = HttpUtil.get("https://api.doctorxiong.club/v1/fund/position",json);
            System.out.println(JSONObject.parseObject(result));
    
            //----------------------------  HttpUtil post  ----------------------------
            JSONObject json = new JSONObject();
            json.put("fundType", Arrays.asList("zs"));
            json.put("sort","z");
            json.put("fundCompany", Arrays.asList("80000248"));
            json.put("pageIndex",1);
            json.put("pageSize",5);
            json.put("Content-Type","application/json;charset=UTF-8");
            String result = HttpUtil.post("https://api.doctorxiong.club/v1/fund/rank", JSONObject.toJSONString(json));
            System.out.println(JSONObject.parseObject(result));
    
            //----------------------------  HttpRequest get  ----------------------------
            HttpResponse httpResponse = HttpRequest.get("https://api.doctorxiong.club/v1/fund/position?code=003634").execute();
            System.out.println(httpResponse);
    
            //----------------------------  HttpRequest post  ----------------------------
            JSONObject json = new JSONObject();
            json.put("fundType",Arrays.asList("zs"));
            json.put("sort","z");
            json.put("fundCompany",Arrays.asList("80000248"));
            json.put("pageIndex",1);
            json.put("pageSize",10);
            HttpResponse httpResponse = HttpRequest.post("https://127.0.0.1:8100/service/test")
                    //.header(Header.USER_AGENT, "Hutool http")//头信息,多个头信息多次调用此方法即可
                    //.setHttpProxy("192.168.1.66", 8032) //配置代理转发
                    .body(JSONObject.toJSONString(json))
                    .contentType("application/json;charset=UTF-8")
                    .timeout(3000)
                    .execute();
            System.out.println("--httpResponse--" + httpResponse);
            if(200 == httpResponse.getStatus() ){
                JSONObject responseBody = JSONObject.parseObject(httpResponse.body());
                System.out.println("--responseBody--" + responseBody);
            }else {
                System.out.println("请求失败。");
            }
    
        }
    
    }
    

      

  • 相关阅读:
    1230: [Usaco2008 Nov]lites 开关灯
    1821: [JSOI2010]Group 部落划分 Group
    1819: [JSOI]Word Query电子字典
    1820: [JSOI2010]Express Service 快递服务
    3038: 上帝造题的七分钟2
    1854: [Scoi2010]游戏
    Codevs3278[NOIP2013]货车运输
    关于使用lazytag的线段树两种查询方式的比较研究
    算法模板——splay区间反转 1
    3223: Tyvj 1729 文艺平衡树
  • 原文地址:https://www.cnblogs.com/zxg-6/p/15194857.html
Copyright © 2020-2023  润新知