ccjgmwz 3 vuotta sitten
vanhempi
commit
83bbe38bf2

+ 5 - 1
unimall-admin-api/pom.xml

@@ -38,7 +38,11 @@
 	</repositories>
 
 	<dependencies>
-
+		<dependency>
+			<groupId>commons-httpclient</groupId>
+			<artifactId>commons-httpclient</artifactId>
+			<version>3.1</version>
+		</dependency>
 		<!-- 引入本地jar -->
 		<dependency>
 			<groupId>com.dangdang.openplatform.openapi</groupId>

+ 69 - 1
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/api/shop/impl/ShopOrderServiceImpl.java

@@ -23,6 +23,12 @@ import com.iotechn.unimall.data.dto.shop.XmlObject.SendGoodsInfo;
 import com.iotechn.unimall.data.mapper.shop.ShopAccountMapper;
 import com.iotechn.unimall.data.mapper.shop.ShopTranMapper;
 import com.iotechn.unimall.data.util.XMLUtil;
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.cookie.CookiePolicy;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.methods.HttpGet;
@@ -250,7 +256,7 @@ public class ShopOrderServiceImpl implements IShopOrderService {
 //            shopAccount.setFuziId(fuziId);
 //        }
         shopAccount = shopAccountMapper.selectOne(shopAccount);
-
+        flushCookie(shopAccount);
         CloseableHttpClient httpClient = HttpClientBuilder.create().build();
         HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/getOrderInfo/?orderId="+orderId);
         try{
@@ -298,6 +304,68 @@ public class ShopOrderServiceImpl implements IShopOrderService {
             return null;
         }
 
+    }
+    public void flushCookie(ShopAccount shopAccount){
+        try{
+            HttpClient httpClient = new HttpClient();
+            GetMethod getMethod = new GetMethod("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
+            // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
+            getMethod.setRequestHeader("cookie", shopAccount.getFuziCookie());
+            // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
+            // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
+            getMethod.setRequestHeader("Referer", "https://shop.kongfz.com/");
+            getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
+            httpClient.executeMethod(getMethod);
+            // 打印出返回数据,检验一下是否成功
+            String text = getMethod.getResponseBodyAsString();
+            try{
+                JSONObject jsonObject = JSONObject.parseObject(text);
+                System.out.println(jsonObject);
+                if(jsonObject.getBoolean("status")){
+                    return;
+                }
+            }
+            catch (Exception exception){
+
+            }
+
+            System.out.println("原cookies = "+shopAccount.getFuziCookie());
+
+            // 登陆 Url
+            String loginUrl = "https://login.kongfz.com/Pc/Login/account";
+            httpClient = new HttpClient();
+            // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
+            PostMethod postMethod = new PostMethod(loginUrl);
+
+            // 设置登陆时要求的信息,用户名和密码
+            NameValuePair[] data = { new NameValuePair("loginName", shopAccount.getFuziAccount()), new NameValuePair("loginPass", shopAccount.getFuziPassword()) };
+            postMethod.setRequestBody(data);
+            try {
+                // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
+                httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+                int statusCode=httpClient.executeMethod(postMethod);
+                text = unicodeToString(postMethod.getResponseBodyAsString());
+                System.out.println("登录返回值 = "+text);
+                if(statusCode == 302){
+                    // 获得登陆后的 Cookie
+                    Cookie[] cookies = httpClient.getState().getCookies();
+                    StringBuffer tmpcookies = new StringBuffer();
+                    for (Cookie c : cookies) {
+                        tmpcookies.append(c.toString() + ";");
+                    }
+                    System.out.println("新cookies = "+tmpcookies.toString());
+                    shopAccount.setFuziCookie(tmpcookies.toString());
+                    shopAccountMapper.updateById(shopAccount);
+                    // 进行登陆后的操作
+                }
+            }
+            catch (Exception e) {
+                e.printStackTrace();
+            }
+        }catch (Exception e) {
+            e.printStackTrace();
+        }
+
     }
     public void dangOrderDeal(ShopAccount shopAccount,ShopOrder shopOrder) throws Exception {
         /**

+ 47 - 38
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/api/user/AdminUserServiceImpl.java

@@ -1,5 +1,10 @@
 package com.iotechn.unimall.admin.api.user;
-
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.cookie.CookiePolicy;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
@@ -16,11 +21,9 @@ import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicHeader;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -31,6 +34,7 @@ import java.io.*;
 import java.net.CookieStore;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -129,46 +133,51 @@ public class AdminUserServiceImpl implements AdminUserService {
     public String test(String param) throws Exception {
 
         String cookieVal = "shoppingCartSessionId=8ca49ed8d30b67e4c4136cfe52ca277f; kfz_uuid=97743462-cbc0-439b-b75d-1aa4bb0939ed; reciever_area=18005000000; PHPSESSID=267e4bbaafbbb5413cd4f7be3e601f79399a2a00; kfz_trace=97743462-cbc0-439b-b75d-1aa4bb0939ed|12179203|01e2236b05e0e5d2|; acw_tc=2760828c16537216057521740ed2e98094ffe4c6944af0e72de8dfc7e09466";
-//        String url = "https://login.kongfz.com/Pc/Login/account";
-//        String username = "天热打空调";
-//        String password = "qq19961218";
-//        Headers.Builder builder = new Headers.Builder();
-//        builder.add("Content-Type", "application/x-www-form-urlencoded");
-//        Headers headers = builder.build();
-//        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
-//                .addFormDataPart("loginName", username)
-//                .addFormDataPart("loginPass", password)
-//                .build();
-//        Response response1 = postData(url, headers, body);
-//        System.out.println(response1.code());
-//        //拿到cookie
-//        String header = response1.header("Set-Cookie");
-//        System.out.println(header);
 
-        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
-        HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/getOrderInfo/?orderId="+param);
-        try{
-            //这里可以设置请求参数,token等
-            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
-            get.addHeader("cookie",cookieVal);
-            HttpResponse response = httpClient.execute(get);//执行获取响应
-            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
-                //返回字符串
-                String res = unicodeToString(EntityUtils.toString(response.getEntity(),"UTF-8"));
-                System.out.println(res);
-                JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
-                Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
-                FuziDTO data = new FuziDTO();
-                if (status) {//返回的状态
-                    data = JSONObject.parseObject(datas.get("data").toString(),FuziDTO.class);//"data"是根据返回值设定
+        // 登陆 Url
+        String loginUrl = "https://login.kongfz.com/Pc/Login/account";
+        // 需登陆后访问的 Url
+//        String dataUrl = "http://hi.mop.com/?";
+        HttpClient httpClient = new HttpClient();
+        GetMethod getMethod = new GetMethod("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
+        // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
+        getMethod.setRequestHeader("cookie", param);
+        // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
+        // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
+        getMethod.setRequestHeader("Referer", "https://shop.kongfz.com/");
+        getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
+        httpClient.executeMethod(getMethod);
+        // 打印出返回数据,检验一下是否成功
+        String text = getMethod.getResponseBodyAsString();
+        JSONObject jsonObject = JSONObject.parseObject(text);
+        System.out.println(jsonObject);
+
+        // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
+        PostMethod postMethod = new PostMethod(loginUrl);
+
+        // 设置登陆时要求的信息,用户名和密码
+        NameValuePair[] data = { new NameValuePair("loginName", "18241771147"), new NameValuePair("loginPass", "Ccj841968545") };
+        postMethod.setRequestBody(data);
+        try {
+            // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
+            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+            int statusCode=httpClient.executeMethod(postMethod);
+            if(statusCode == 302){
+                // 获得登陆后的 Cookie
+                Cookie[] cookies = httpClient.getState().getCookies();
+                StringBuffer tmpcookies = new StringBuffer();
+                cookieVal = "";
+                for (Cookie c : cookies) {
+                    tmpcookies.append(c.toString() + ";");
                 }
-                return res;
+                cookieVal = tmpcookies.toString();
+                System.out.println("模拟登录成功"+cookieVal);
+                // 进行登陆后的操作
             }
-
-        } catch (IOException e) {
+        }
+        catch (Exception e) {
             e.printStackTrace();
         }
-
         return cookieVal;
     }
     /**