java post 請求

yemaiqingxiang發表於2019-03-25
        ResponseEntity<Map> responseEntity = null;
        try {
            Map<String, String> body = new HashMap<>();
            body.put("username", USER_NAME);
            body.put("password", PASSWORD);
            RestTemplate template = new RestTemplate();
            RequestEntity<Map> requestEntity = RequestEntity
                    .post(new URI(LOGIN_URL))
                    .header("Content-Type", "application/json")
                    .header("Accept", "application/json")
                    .header("cache-control", "no-cache")
                    .accept(MediaType.APPLICATION_JSON)
                    .body(body);
            responseEntity = template.exchange(requestEntity, Map.class);
        } catch (Exception e) {

        }
        int codeValue = responseEntity.getStatusCodeValue();
        if (codeValue == 200) {
            TOKEN = responseEntity.getBody().get("token").toString();
        }
        return TOKEN;
複製程式碼

相關文章