java rest-assure-get请求和身份验证令牌

jljoyd4f  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(177)

我试图创建一个带有auth令牌的post请求,但得到的响应为空。你能帮帮我吗?你能让我知道我在代码里遗漏了什么吗?

public static void main(String[] args) {
         try {
                String url = "https://test.abc.com/";
                    String token="XXXXXX-abcd-496a-ae73-7659587896";
                URL object = new URL(url);
                HttpURLConnection con = (HttpURLConnection) object.openConnection();
                con.setDoInput(true);
                con.setRequestMethod("GET");
                con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                con.setRequestProperty("Accept", "application/json");
                con.setRequestProperty("Authorization", "Bearer " + token);
                //Display what the GET request returns
                StringBuilder sb = new StringBuilder();
                int HttpResult = con.getResponseCode();
                if (HttpResult == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(con.getInputStream(), "utf-8"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
                    System.out.println(con.getResponseMessage());
                } else {
                    System.out.println(con.getResponseMessage());
                }
            } catch (Exception e) {
                System.out.println(e);
            }
         }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题