HttpUtil.java
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.example.mypulsar.utils;
import com.example.mypulsar.bean.Command;
import com.example.mypulsar.bean.TuYaAirCondition;
import com.example.mypulsar.bean.TuYaCommand;
import org.apache.pulsar.shade.com.google.gson.Gson;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* http 工具类
*/
public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/json";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
public static boolean controlDev(String deviceId, String code, String value) {
String url = "http://120.26.116.253:5555/TuYa/sendDeviceCommandsFun?deviceId=" + deviceId;
RestTemplate restTemplate = new RestTemplate();
TuYaCommand tuYaCommand = new TuYaCommand();
List<Command> commandList = new ArrayList<>();
Command command = new Command();
command.setCode(code);
command.setValue(value);
commandList.add(command);
tuYaCommand.setCommands(commandList);
String responseEntity = restTemplate.postForObject(url, tuYaCommand, String.class);
System.out.println("tuYaCommand:" + tuYaCommand.toString());
System.out.println("responseEntity:" + responseEntity);
return responseEntity.equals("1");
}
public static boolean IsDeviceOnline(String deviceId) {
String url = "http://121.40.109.21:8089/file/getCard";
RestTemplate restTemplate = new RestTemplate();
Map<String, Object> map = new HashMap<>();
map.put("deviceId", deviceId);
String res = restTemplate.getForObject(url, String.class, map);
return res.equals("1");
}
public static String addAirCode(String conValue, String conDevId) {
String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode", conDevId);
RestTemplate restTemplate = new RestTemplate();
TuYaAirCondition tuYaAirCondition = new TuYaAirCondition();
try {
JSONObject jsonObject = new JSONObject(conValue);
tuYaAirCondition.setMode(jsonObject.getString("mode"));
tuYaAirCondition.setPower(jsonObject.getString("power"));
tuYaAirCondition.setRemote_id(jsonObject.getString("remote_index"));
tuYaAirCondition.setWind(jsonObject.getString("wind"));
tuYaAirCondition.setTemp(jsonObject.getString("temp"));
tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index"));
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("url-------------------------------:" + url+" conValue:"+tuYaAirCondition.toString());
String response = restTemplate.postForObject(url, tuYaAirCondition, String.class);
System.out.println("response----------------------------------:" + response);
return response;
}
}