Send HTTP Post Payload with Java
29,984
Basically, you can do it with the standard Java API. Check out URL
, URLConnection
, and maybe HttpURLConnection
. They are in package java.net
.
As to the API specific signature, try sStringToHMACMD5
found in here.
And remember to CHANGE YOUR API KEY, this is very IMPORTANT, since everyone knows it know.
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();
Author by
Kooshiar Azimian
Updated on November 20, 2020Comments
-
Kooshiar Azimian over 3 years
I'm trying to connect to the grooveshark API, this is the http request
POST URL http://api.grooveshark.com/ws3.php?sig=f699614eba23b4b528cb830305a9fc77 POST payload {"method":'addUserFavoriteSong",'parameters":{"songID":30547543},"header": {"wsKey":'key","sessionID":'df8fec35811a6b240808563d9f72fa2'}}
My question is how can I send this request via Java?
-
Kooshiar Azimian over 11 yearsPostMethod doesn't exist in HttpClient 4.2.2
-
Anders R. Bystrup over 11 yearsYou will note that the link is for the 3.x version. I'm quite sure something similar exists in 4.2.2 and anyway, you are expected to provide some work yourself, given the guidance from others here :-) Cheers
-
xiaofeng.li over 11 yearsYes, it can and should be a
String
in this case that we are usingPrintWriter
. -
Kooshiar Azimian over 11 yearsI did that but got the same error "method not defined", I defined String data = "{'method': 'addUserFavoriteSong', 'parameters': {'songID': 0}, 'header': {'wsKey': 'key', 'sessionID': 'sessionID'}}";
-
xiaofeng.li over 11 yearsDoes the API documentation mention anything specific about the HTTP request? Like what the headers should be, e.g. 'Content-Type'?
-
xiaofeng.li over 11 yearsBTW, standard JSON data should always use double-quote
"
to denote keys and String literals. -
Kooshiar Azimian over 11 yearsthanks a lot, replaced ' with \" and don't get than error anymore. But get signature error for: String data = "{\"method\":\"getArtistSearchResults\",\"parameters\":{\"query\":\"adele\"},\"header\":{\"wsKey\":\"concertboom\",\"sessionID\":\"0\"}}"; String key = "b07cd9d001e6895956023687e4629f60"; String signature = hash(key, data);
-
xiaofeng.li over 11 yearsHow is your
hash
method implemented? I found a PHP client from grooveshark's developer website, it must be a Java equivalent version of thehash_hmac
in PHP.