英文:
How to send a correct HTTP PUT request in Java
问题
我想用Java使用Philips HUE Api关闭灯光。
为此,我有以下API:http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/
这将返回:
{
"state": {
"on": true,
"bri": 178,
"hue": 41044,
"sat": 56,
"effect": "none",
"xy": [
0.3313,
0.3447
],
"ct": 181,
"alert": "select",
"colormode": "ct",
"mode": "homeautomation",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2020-07-03T12:17:24"
},
"type": "Extended color light",
"name": "Hue color spot 1",
"modelid": "LCG002",
"manufacturername": "Signify Netherlands B.V.",
"swconfigid": "9FD98F72"
}
现在我想用Java将 "on: true"
修改为 "on: false"
。为此,我有以下代码:
URL url = new URL("http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.setRequestProperty("Content-Type", "application/json");
JSONObject requestBody = new JSONObject();
requestBody.put("on", false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(requestBody.toString());
writer.flush();
writer.close();
// Debug
int response = connection.getResponseCode();
System.out.println(requestBody.toString());
System.out.println(response);
这将返回 '200'
和 { "on": false }
,看起来很完美。
但是由于某种原因,它实际上什么都没做。尽管连接存在,灯仍然保持开启。
有什么想法为什么会这样吗?
英文:
I want to turn my lights off with Java using the Philips HUE Api.
For this, I have the following API: http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/
This returns:
{
"state": {
"on": true,
"bri": 178,
"hue": 41044,
"sat": 56,
"effect": "none",
"xy": [
0.3313,
0.3447
],
"ct": 181,
"alert": "select",
"colormode": "ct",
"mode": "homeautomation",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2020-07-03T12:17:24"
},
"type": "Extended color light",
"name": "Hue color spot 1",
"modelid": "LCG002",
"manufacturername": "Signify Netherlands B.V.",
"swconfigid": "9FD98F72"
}
Now I want to modify the "on: true" to "on: false" with Java. For that, I have this code:
URL url = new URL("http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.setRequestProperty("Content-Type", "application/json");
JSONObject requestBody = new JSONObject();
requestBody.put("on", false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(requestBody.toString());
writer.flush();
writer.close();
// Debug
int response = connection.getResponseCode();
System.out.println(requestBody.toString());
System.out.println(response);
This returns '200' and {"on":false} which would be perfect.
But for some reason, it just does nothing. The light remains on even though the connection exists.
Any ideas why?
答案1
得分: 1
你正在使用错误的 URL 进行 PUT 请求。
对 http://192.168.0.53/api/.../lights/1
的 GET 请求将返回当前状态。
但是要进行修改,您需要向 http://192.168.0.53/api/.../lights/1/state
发送 PUT 请求。
英文:
You are using the wrong URL for the PUT request.
A GET request to http://192.168.0.53/api/.../lights/1
will return the current state
But to modify you need a PUT request to http://192.168.0.53/api/.../lights/1/state
答案2
得分: 0
我正在尝试做同样的事情,但不清楚Java部分。
我已经在命令级别上使用三个引号使curl工作:
curl -X PUT -H "Content-Type: application/json" -d {"on":false} http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/state
这段代码将适用于你的Hue灯,当你成功实现后请与我分享Java代码。mike@toggle.is
英文:
I am trying to do the same thing, but don't know the Java part.
I got curl to work at the command level with triple quotes:
curl -X PUT -H "Content-Type: application/json" -d {"""on""":false} http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/state
This code will work with your hue lights, Please share the Java code with me when you get it to work. mike@toggle.is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论