使用Java发送HTTP Post Payload [英] Send HTTP Post Payload with Java

查看:972
本文介绍了使用Java发送HTTP Post Payload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到groovehark API,这是http请求

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'}}

我的问题是如何通过Java发送此请求?

My question is how can I send this request via Java?

推荐答案

基本上,您可以使用标准Java API来完成查看 URL URLConnection ,也许 HttpURLConnection 。它们在包 java.net

Basically, you can do it with the standard Java API. Check out URL, URLConnection, and maybe HttpURLConnection. They are in package java.net.

关于API特定签名,请尝试在 sStringToHMACMD5 。 com / questions / 8396297 / android-how-to-create-hmac-md5-string>这里。

As to the API specific signature, try sStringToHMACMD5 found in here.

并记得改变你的API KEY,这是非常重要的,因为每个人都知道它。

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();

这篇关于使用Java发送HTTP Post Payload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆