HTTP头代码验证HTTP返回令牌java代码中的错误

时间:2016-07-29 06:33:18

标签: java rest http authentication

使用密钥访问URL并尝试从URL获取令牌,如下所示的java代码,在此我在HTTP响应返回部分中收到错误

public static String getResponseFromURL() {
        String jsonResponse = null;

        try {
               URL url = new URL("https://yyy.com/auth/accesstoken");

            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            // int timeOut = connection.getReadTimeout();
            connection.setReadTimeout(60 * 1000);
            connection.setConnectTimeout(60 * 1000);
            String authorization = "Yv7eNLKW2:xtbIjOX";
            String encodedAuth = "Basic " + Base64.encode(authorization.getBytes());

            connection.setRequestProperty("Authorization", encodedAuth);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            String body = "grant_type=client_credentials";
            connection.setRequestMethod("POST");

            int responseCode = connection.getResponseCode();
            //String responseMsg = connection.getResponseMessage();           

            if (responseCode == 200) {
                InputStream inputStr = connection.getInputStream();
                String encoding = connection.getContentEncoding() == null ? "UTF-8"
                        : connection.getContentEncoding();
                jsonResponse = IOUtils.toString(inputStr, encoding);


            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return jsonResponse;

    }

代码错误

    method toString in class Object cannot be applied to given types;
  required: no arguments
  found: InputStream,String
  reason: actual and formal argument lists differ in length

 jsonResponse = IOUtils.toString(inputStr, encoding);

0 个答案:

没有答案