我试图将Genius API集成到我的Java程序中,但我并不完全确定我在做出实际的HTTP请求时做了什么。 这是我尝试使用的代码:
URLConnection connection = new URL("https://api.genius.com/search?q=juice").openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer TOKEN");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
(其中TOKEN是我的Genius身份验证令牌)
但是我收到了一封带有403禁止消息的IOException。
java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.genius.com/search?q=juice
当我尝试使用Hurl.it时,同样的请求也有效:
所以我不确定这里发生了什么。当我尝试打印标题以查看它们是否通过时,我得到了这个:
System.out.println("auth: " + connection.getHeaderField("Authorization"));
System.out.println("content: " + connection.getHeaderField("Content-Type"));
/** output */
auth: null
content: text/html; charset=UTF-8
我很感激这里的任何帮助 - 谢谢!!