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();
Share:
29,984
Kooshiar Azimian
Author by

Kooshiar Azimian

Updated on November 20, 2020

Comments

  • Kooshiar Azimian
    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
    Kooshiar Azimian over 11 years
    PostMethod doesn't exist in HttpClient 4.2.2
  • Anders R. Bystrup
    Anders R. Bystrup over 11 years
    You 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
    xiaofeng.li over 11 years
    Yes, it can and should be a String in this case that we are using PrintWriter.
  • Kooshiar Azimian
    Kooshiar Azimian over 11 years
    I 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
    xiaofeng.li over 11 years
    Does the API documentation mention anything specific about the HTTP request? Like what the headers should be, e.g. 'Content-Type'?
  • xiaofeng.li
    xiaofeng.li over 11 years
    BTW, standard JSON data should always use double-quote " to denote keys and String literals.
  • Kooshiar Azimian
    Kooshiar Azimian over 11 years
    thanks a lot, replaced ' with \" and don't get than error anymore. But get signature error for: String data = "{\"method\":\"getArtistSearchResults\",\"parameters\":{\"qu‌​ery\":\"adele\"},\"h‌​eader\":{\"wsKey\":\‌​"concertboom\",\"ses‌​sionID\":\"0\"}}"; String key = "b07cd9d001e6895956023687e4629f60"; String signature = hash(key, data);
  • xiaofeng.li
    xiaofeng.li over 11 years
    How is your hash method implemented? I found a PHP client from grooveshark's developer website, it must be a Java equivalent version of the hash_hmac in PHP.