1、开锁代码
public static void main(String[] args) {
String url = "https://api.parkline.cc/api/devicecgi";
HashMap hm = new HashMap();//封装传递参数
hm.put("token", "bhmhL6ap/xZsWEFby2qnwF2dJResJ763nXPzLEqsj4mvNLk5ccX18v5WSNPPN9fOz+Y3/+y2bD8=");
hm.put("devid", "0008");
hm.put("typeid", "01");
hm.put("lockid", "01");
System.out.println(httpUrlConnection(url, hm));// 调用发送请求方法
}
===============================================================================
//字符传递并返回方法
private static String httpUrlConnection(String pathurl,HashMap hm) {
String result = null;
try {
URL url = new URL(pathurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("referer", "http://192.168.4.164");//后台填写的授权接入地址,必须包含http或https协议
conn.setRequestMethod("POST");
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.print(getParams(hm));
pw.flush();
pw.close();
if (conn.getResponseCode() == 200) {
StringBuffer sb = new StringBuffer();
String readLine;
BufferedReader responseReader;
responseReader = new BufferedReader(new InputStreamReader(conn
.getInputStream(), "utf-8"));
while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine);
}
responseReader.close();
result=sb.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
===============================================================================
// 参数处理方法
public static String getParams(Map paramValues)
{
String params = "";
String beginLetter = "";
Set key = paramValues.keySet();
try {
for (Iterator it = key.iterator(); it.hasNext();)
{
String s = (String) it.next();
if (params.equals("")) {
params += beginLetter + s + "="
+ URLEncoder.encode(paramValues.get(s), "UTF-8");
} else {
params += "&" + s + "="
+ URLEncoder.encode(paramValues.get(s), "UTF-8");
}
}
} catch (Exception e) {
}
return params;
}
提示:其他功能请参考开锁代码