我正在尝试连接到groovehark API,这是http请求
POST URL
http://api.grooveshark.com/ws3.PHP?sig=f699614eba23b4b528cb830305a9fc77
POST payload
{"method":'addUserFavoriteSong",'parameters":{"songID":30547543},"header":
{"wsKey":'key","sessionID":'df8fec35811a6b240808563d9f72fa2'}}
我的问题是如何通过Java发送此请求?
最佳答案
基本上,您可以使用标准Java API来完成它.查看
原文链接:https://www.f2er.com/java/438243.htmlURL
,URLConnection
,也许HttpURLConnection
.它们在java.net
包中.
至于API特定签名,请尝试在here.中找到的sStringToHMACMD5
并记住改变你的API密钥,这是非常重要的,因为每个人都知道它知道.
String payload = "{\"method\": \"addUserFavoriteSong\",....}";
String key = ""; // Your api key.
String sig = sStringToHMACMD5(payload,key);
URL url = new URL("http://api.grooveshark.com/ws3.PHP?sig=" + sig);
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
OutputStream os = connection.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
pw.write(payload);
pw.close();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
String response = sb.toString();