使用密钥访问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);