瀏覽代碼

接口对接

yumeng 5 年之前
父節點
當前提交
8d1fb50f53

+ 107 - 0
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,17 +1,124 @@
 package org.jeecg;
 
 import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
 @RunWith(SpringRunner.class)
 @SpringBootTest
 @Slf4j
 public class SampleTest {
     @Value("${jeecg.path.chrome-driver}")
     private String chromeDriver;
+
+    @Test
+    public void kuaisShouReport() {
+
+
+        try {
+          /*  String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/create";
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", " application/json");
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            JSONObject param = new JSONObject();
+            param.put("advertiser_id", 142402);
+            param.put("task_name", "142402-test");
+
+            JSONObject taskParams = new JSONObject();
+            taskParams.put("start_date", "2020-01-01");
+            taskParams.put("end_date", "2020-03-05");
+            taskParams.put("view_type", 1);
+            param.put("task_params",taskParams);
+
+
+
+            String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            System.err.println(resultJson);*/
+
+            // 1229917  142402
+
+
+
+         /*   String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list";
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", " application/json");
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            JSONObject param = new JSONObject();
+            param.put("advertiser_id", 142402);
+            JSONArray taskIds = new JSONArray();
+            taskIds.add(1229917);
+
+            param.put("task_ids", taskIds);
+            String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            System.err.println(resultJson);*/
+
+
+          /*  String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download";
+
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+
+            Map<String,Object> param = new HashMap<>();
+            param.put("advertiser_id", 142402);
+            param.put("task_id", "1229917");
+            String result = HttpUtils.KuaiShouttpGetRequest(url, param,headers);*/
+
+
+            URL url = new URL("https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download?task_id=1229917&advertiser_id=142402");
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            //设置超时间为3秒
+            conn.setConnectTimeout(10 * 1000);
+            //防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            conn.setRequestProperty("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            //得到输入流
+            InputStream inputStream = conn.getInputStream();
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            //文件保存位置
+            File saveDir = new File("D:\\file");
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            File file = new File(saveDir + File.separator + "qqww.csv");
+            FileOutputStream fos = new FileOutputStream(file);
+            fos.write(getData);
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+
+
+        } catch (Exception e) {
+            e.printStackTrace();
+
+        }
+
+
+    }
+
+    public static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
+    }
 }
 
 

+ 90 - 4
module-common/src/main/java/cn/com/ctop/common/module/utils/HttpUtils.java

@@ -167,7 +167,7 @@ public class HttpUtils {
         String strReturn = "";
         try {
             HttpPost httppost = new HttpPost(url);
-            if(headers != null){
+            if (headers != null) {
                 for (String key : headers.keySet()) {
                     httppost.setHeader(key, headers.get(key));
                 }
@@ -207,7 +207,7 @@ public class HttpUtils {
         try {
             HttpPost httppost = new HttpPost(url);
             httppost.setHeader("User-Agent", USER_AGENT);
-            if(headers != null){
+            if (headers != null) {
                 for (String key : headers.keySet()) {
                     httppost.setHeader(key, headers.get(key));
                 }
@@ -257,8 +257,8 @@ public class HttpUtils {
             if (param != null && param.size() > 0) {
                 httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
             }
-            RequestConfig requestConfig =  RequestConfig.custom().setSocketTimeout(120 * 1000).setConnectTimeout(120 * 1000).build();
-                httppost.setConfig(requestConfig);
+            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120 * 1000).setConnectTimeout(120 * 1000).build();
+            httppost.setConfig(requestConfig);
             HttpEntity respentity;
 
             HttpResponse response = httpClient.execute(httppost);
@@ -685,4 +685,90 @@ public class HttpUtils {
         }
         return result;
     }
+
+
+    public static JSONObject kuaiShouHttpGetRequest(String accessToken, String url, JSONObject params) {
+        // 构造请求
+        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
+            @Override
+            public String getMethod() {
+                return "GET";
+            }
+        };
+        httpEntity.setHeader("Access-Token", accessToken);
+        CloseableHttpResponse response = null;
+        CloseableHttpClient client = null;
+        try {
+            client = HttpClientBuilder.create().build();
+            httpEntity.setURI(URI.create(url));
+            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
+            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
+            httpEntity.setConfig(requestConfig);
+            response = client.execute(httpEntity);
+            if (response != null && response.getStatusLine().getStatusCode() == 200) {
+                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+                StringBuilder result = new StringBuilder();
+                String line = "";
+                while ((line = bufferedReader.readLine()) != null) {
+                    result.append(line);
+                }
+                bufferedReader.close();
+                return JSONObject.parseObject(result.toString());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                if (client != null) {
+                    client.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
+
+    public static String KuaiShouttpGetRequest(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
+        HttpClient httpclient = getHttpclient();
+        String strReturn = "";
+        try {
+
+
+            StringBuilder postBody = new StringBuilder();
+            for (Map.Entry<String, Object> entry : params.entrySet()) {
+                if (entry.getValue() == null) {
+                    continue;
+                }
+                postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
+                        "utf-8")).append("&");
+            }
+            if (!params.isEmpty()) {
+                postBody.deleteCharAt(postBody.length() - 1);
+            }
+
+            HttpGet httpget = new HttpGet(url + "?" + postBody);
+            System.err.println(url + postBody);
+            if (headers != null) {
+                Iterator<String> keyIter = headers.keySet().iterator();
+                while (keyIter.hasNext()) {
+                    String curKey = keyIter.next();
+                    if (curKey != null && headers.get(curKey) != null) {
+                        httpget.addHeader(curKey, headers.get(curKey));
+                    }
+                }
+            }
+            HttpEntity respentity;
+            HttpResponse response = httpclient.execute(httpget);
+            respentity = response.getEntity();
+            strReturn = EntityUtils.toString(respentity);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return strReturn;
+    }
 }