|
@@ -0,0 +1,161 @@
|
|
|
+package com.ruoyi.resource.miniapp;
|
|
|
+
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.utils.URIBuilder;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import static com.ruoyi.resource.miniapp.ApiUtils.getTimeOffset;
|
|
|
+import static com.ruoyi.resource.miniapp.ApiUtils.sign;
|
|
|
+
|
|
|
+public class ApiExample {
|
|
|
+ public static void main(String args[]) throws Exception {
|
|
|
+ httpGetExample();
|
|
|
+ httpPostExample();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 电信云平台 -- http GET请求示例
|
|
|
+ */
|
|
|
+ public static void httpGetExample() throws Exception {
|
|
|
+ String secret = "FJDq8agNp5";// 密钥,到控制台->应用管理打开应用可以找到此值
|
|
|
+ String application = "91Ebv1S0HBb";// appKey,到应用管理打开应用可以找到此值
|
|
|
+ String version = "20181031202055";// api版本,到文档中心->使能平台API文档打开要调用的api可以找到此值
|
|
|
+ String MasterKey = "25ce00cc28c1498c833276110ee483f0";// MasterKey,在产品中心打开对应的产品查看此值
|
|
|
+ HttpResponse response = null;
|
|
|
+ CloseableHttpClient httpClient = null;
|
|
|
+ httpClient = HttpClientBuilder.create().build();
|
|
|
+ long offset = getTimeOffset();// 获取时间偏移量,方法见前面
|
|
|
+ // 下面示例以根据产品ID查询产品信息的API为例【具体信息请以使能平台的API文档为准】。
|
|
|
+ // 构造请求的URL,具体参考文档中心->API文档中的请求地址和访问路径
|
|
|
+ URIBuilder uriBuilder = new URIBuilder();
|
|
|
+ uriBuilder.setScheme("https");// 请求用的协议,http或者https
|
|
|
+ uriBuilder.setHost("ag-api.ctwing.cn/aep_product_management");//请求地址
|
|
|
+ uriBuilder.setPath("/product");//访问路径,可以在API文档中对应的API中找到此访问路径
|
|
|
+ // 在请求的URL中添加参数,具体参考文档中心->API文档中“请求参数”说明
|
|
|
+ // (如果有MasterKey,将MasterKey加到head中,不加在此处)
|
|
|
+ uriBuilder.addParameter("productId", "9392");
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());//构造get请求
|
|
|
+ long timestamp = System.currentTimeMillis() + offset;// 获取时间戳
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
|
|
+ dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
+ String dataString = dateFormat.format(date);// 生成格式化的日期字符串
|
|
|
+ // head中添加公共参数,具体参考文档中心->API文档中“公共参数”里的公共请求参数
|
|
|
+ // httpGet.addHeader("MasterKey", MasterKey);// MasterKey加在此处head中
|
|
|
+ httpGet.addHeader("application", application);
|
|
|
+ httpGet.addHeader("timestamp", "" + timestamp);
|
|
|
+ httpGet.addHeader("version", version);
|
|
|
+ httpGet.addHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
+ httpGet.addHeader("Date", dataString);
|
|
|
+ // 下列注释的head暂时未用到
|
|
|
+ // httpGet.addHeader("sdk", "GIT: a4fb7fca");
|
|
|
+ // httpGet.addHeader("Accept", "gzip,deflate");
|
|
|
+ // httpGet.addHeader("User-Agent", "Telecom API Gateway Java SDK");
|
|
|
+ // 构造签名需要的参数,如果参数中有MasterKey,则添加来参与签名计算,其他参数根据实际API从URL中获取
|
|
|
+ Map<String, String> param = new HashMap<String, String>();
|
|
|
+ // param.put("MasterKey", MasterKey);
|
|
|
+ // 从URL中获取参数加到param中
|
|
|
+ List<NameValuePair> list = uriBuilder.getQueryParams();
|
|
|
+ for (int i = 0; i < list.size(); i++)
|
|
|
+ param.put(list.get(i).getName(), list.get(i).getValue());
|
|
|
+ // 添加签名
|
|
|
+ httpGet.addHeader("signature", sign(param, timestamp, application, secret, null));
|
|
|
+ System.out.println(httpGet.getURI());
|
|
|
+ try {
|
|
|
+ // 发送请求
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ // 从response获取响应结果
|
|
|
+ System.out.println(new String(EntityUtils.toByteArray(response.getEntity())));
|
|
|
+ httpClient.close();
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 电信云平台 -- http POST请求示例
|
|
|
+ */
|
|
|
+ public static void httpPostExample() throws Exception {
|
|
|
+// // CH的
|
|
|
+// String secret = "3Ee7KJr5pe";
|
|
|
+// String application = "BSdpzh2hBl3";
|
|
|
+// String version = "20181031202156";
|
|
|
+// String MasterKey = "8dd6dbf8035d4a0da6974bf3c48d9daa";
|
|
|
+ String secret = "FJDq8agNp5";//密钥,到控制台->应用管理打开应用可以找到此值
|
|
|
+ String application = "91Ebv1S0HBb";//appKey,到控制台->应用管理打开应用可以找到此值
|
|
|
+ String version = "20181031202117";//api版本,到文档中心->使能平台API文档打开要调用的api可以找到版本值
|
|
|
+ String MasterKey = "25ce00cc28c1498c833276110ee483f0";//MasterKey,在产品中心打开对应的产品查看此值
|
|
|
+ // 下面以增加设备的API为例【具体信息请以使能平台的API文档为准】。
|
|
|
+ //请求BODY,到文档中心->使能平台API文档打开要调用的api中,在“请求BODY”中查看
|
|
|
+ String bodyString = "{\"deviceName\":\"testDevice\",\"deviceSn\":\"\",\"imei\":123456789012345,\"operator\":\"admin\",\"productId\":\"9392\"}";
|
|
|
+ CloseableHttpClient httpClient = null;
|
|
|
+ HttpResponse response = null;
|
|
|
+ httpClient = HttpClientBuilder.create().build();
|
|
|
+ long offset = getTimeOffset();// 获取时间偏移量,方法见前面
|
|
|
+ // 构造请求的URL,具体参考文档中心->使能平台API文档中的请求地址和访问路径
|
|
|
+ URIBuilder uriBuilder = new URIBuilder();
|
|
|
+ uriBuilder.setScheme("https");
|
|
|
+ uriBuilder.setHost("ag-api.ctwing.cn/aep_device_management"); //请求地址
|
|
|
+ uriBuilder.setPath("/device"); //访问路径,可以在API文档中对应API中找到此访问路径
|
|
|
+ // 在请求的URL中添加参数,具体参考文档中心->API文档中请求参数说明
|
|
|
+ // (如果有MasterKey,将MasterKey加到head中,不加在此处)
|
|
|
+ //uriBuilder.addParameter("productId", "9392");//如果没有其他参数,此行不要
|
|
|
+ HttpPost httpPost = new HttpPost(uriBuilder.build());//构造post请求
|
|
|
+ long timestamp = System.currentTimeMillis() + offset;// 获取时间戳
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
|
|
+ dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
+ String dataString = dateFormat.format(date);// 生成格式化的日期字符串
|
|
|
+ // head中添加公共参数
|
|
|
+ httpPost.addHeader("MasterKey", MasterKey);// MasterKey加在此处head中
|
|
|
+ httpPost.addHeader("application", application);
|
|
|
+ httpPost.addHeader("timestamp", "" + timestamp);
|
|
|
+ httpPost.addHeader("version", version);
|
|
|
+ httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
+ httpPost.addHeader("Date", dataString);
|
|
|
+ // 下列注释的head暂时未用到
|
|
|
+ // httpPost.addHeader("sdk", "GIT: a4fb7fca");
|
|
|
+ // httpPost.addHeader("Accept", "gzip,deflate");
|
|
|
+ // httpPost.addHeader("User-Agent", "Telecom API Gateway Java SDK");
|
|
|
+ // 构造签名需要的参数,如果参数中有MasterKey,则添加来参与签名计算,
|
|
|
+ // 其他参数根据实际API从URL中获取,如有其他参数,写法参考get示例
|
|
|
+ Map<String, String> param = new HashMap<String, String>();
|
|
|
+ param.put("MasterKey", MasterKey);
|
|
|
+ // 添加签名
|
|
|
+ httpPost.addHeader("signature", sign(param, timestamp, application, secret, bodyString.getBytes()));
|
|
|
+ //请求添加body部分
|
|
|
+ httpPost.setEntity(new StringEntity(bodyString,"utf-8"));
|
|
|
+ try {
|
|
|
+ // 发送请求
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ // 从response获取响应结果
|
|
|
+ System.out.println(new String(EntityUtils.toByteArray(response.getEntity())));
|
|
|
+ httpClient.close();
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|