https获取数据

使用hutool工具

  • 第一种情况,参数都是简单的参数没有特殊符号,这个时候的尝试可以用一个JSONObject来存放,因为.body()里面的参数接收的是string类型,所有需要json.toJSONString()这一过程

    JSONObject json = new JSONObject();
            json.put("flightNo", flightInfo.getFlightNo());
            json.put("flightDate", flightInfo.getFlightDate());
            String url = "https://xxxx/getInquiryDetailList";
            return HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .body(json.toJSONString())
                    .execute().body();
    
  • 第二种情况,参数里面有特殊的字符,这个时候特殊字符要先做encode

     String url = "https://xxxx";
            String searchword = URLUtil.encode(enterpriseName);
            // body里面有特殊字符,不能直接用,要转义(urlencode)后使用
            String body = "conditions=%7B%22excep_tab%22%3A%220%22%2C%22ill_tab%22%3A%220%22%2C%22area%22%3A%220%22%2C%22cStatus%22%3A%220%22%2C%22xzxk%22%3A%220%22%2C%22xzcf%22%3A%220%22%2C%22dydj%22%3A%220%22%7D&searchword=" + searchword + "&sourceType=W";
            String host = HttpRequest.post(url)
                    .header("host", "app.gsxt.gov.cn")
                    .header("connection", "keep-alive")
                    .header("content-Length", String.valueOf(body.trim().length()))
                    .header("accept", "application/json")
                    .header("user-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat")
                    .header("content-type", "application/x-www-form-urlencoded")
                    .body(body)
                    .execute().body();
    
  • 这个可以参考postman,因为postman也会做一次encode,下面是postman做encode之前的样式

image-20220705000748446

  • 下面是postman做encode之后的样式

image-20220705000855031

image-20220705000919201

posted @ 2022-07-14 19:02  小猫爱哭鬼  阅读(111)  评论(0编辑  收藏  举报