java - 使用 Java 发送 HTTP Post Payload

标签 java httprequest payload grooveshark

我正在尝试连接到 grooveshark 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 来完成。查看URL , URLConnection ,也许 HttpURLConnection .它们在包装中 java.net .

关于 API 特定签名,请尝试在 here. 中找到的 sStringToHMACMD5

并记得更改您的 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 - 使用 Java 发送 HTTP Post Payload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396171/

相关文章:

java - 在 Servlet 中获取 HTTP 和 HTTPS 请求的完整 URL 和查询字符串

ios - 有时未收到 Apple 推送通知 (JavaPNS)

Java switch 与 for 循环性能

java - Android 中通过向 url 传递参数来检索数据

java - 如何检查数据库中是否存在 Oracle View ?在执行查询之前

testing - 如何为 Salesforce 进行 MultiMock Http 标注测试?

ios - 如何在iPCU中设置MDM Payload的 "Identity"?

rest - 哪个http动词/最佳实践来更新实体而无需发送有效负载?

java - 如何从容器外部访问Websphere 7.0中定义的JNDI命名空间?

java - 没有 Spring MVC 的 Spring Web 上下文