ccjgmwz 3 yıl önce
ebeveyn
işleme
0dc41caa3b

+ 12 - 24
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/AdminCheckQuartz.java

@@ -264,7 +264,7 @@ public class AdminCheckQuartz {
             // 登陆 Url
             String loginUrl = "https://login.kongfz.com/Pc/Login/account";
             BasicCookieStore store= new BasicCookieStore();
-            httpClient = HttpClients.custom().setDefaultCookieStore(store).build();
+            httpClient =HttpClients.custom().setDefaultCookieStore(store).build();
             // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
             HttpPost httpPost = new HttpPost(loginUrl);
 
@@ -281,31 +281,18 @@ public class AdminCheckQuartz {
 
             // 执行请求操作,并拿到结果(同步阻塞)
             response = httpClient.execute(httpPost);
-            // 获取结果实体
-            HttpEntity entity = response.getEntity();
-            if (entity != null) {
-                String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
-                //返回字符串
-                String res = unicodeToString(strTmp);
-                JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
-                Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
-                if(status){
-                    List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
-                    String nCookie = "";
-                    for(org.apache.http.cookie.Cookie cookie: cookielist){
-                        String name=cookie.getName();
-                        String value=cookie.getValue();
-                        nCookie += name + "="+value+";";
-                    }
-                    System.out.println("nCookie:" + nCookie);
-                    shopAccount.setFuziCookie(nCookie);
-                    shopAccountMapper.updateById(shopAccount);
-                }
-                else{
-                    System.out.println("登录返回值:" + res);
+            if(response.getStatusLine().getStatusCode() == 302){
+                List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
+                String nCookie = "";
+                for(org.apache.http.cookie.Cookie cookie: cookielist){
+                    String name=cookie.getName();
+                    String value=cookie.getValue();
+                    nCookie += name + "="+value+";";
                 }
+                System.out.println("nCookie:" + nCookie);
+                shopAccount.setFuziCookie(nCookie);
+                shopAccountMapper.updateById(shopAccount);
             }
-            EntityUtils.consume(entity);
             // 释放链接
             response.close();
 
@@ -314,6 +301,7 @@ public class AdminCheckQuartz {
         }
 
     }
+
     /**
      * Unicode转 汉字字符串
      *

+ 57 - 41
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/api/shop/impl/ShopOrderServiceImpl.java

@@ -31,9 +31,15 @@ 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.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+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.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.apache.ibatis.session.RowBounds;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -312,66 +318,76 @@ public class ShopOrderServiceImpl implements IShopOrderService {
     }
     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();
+            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+            HttpGet getMethod = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
+            getMethod.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");
+            getMethod.addHeader("cookie",shopAccount.getFuziCookie());
+
+            httpClient.execute(getMethod);
+            CloseableHttpResponse response = httpClient.execute(getMethod);//执行获取响应
             try{
-                JSONObject jsonObject = JSONObject.parseObject(text);
-                System.out.println(jsonObject);
-                if(jsonObject.getBoolean("status")){
-                    return;
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//根据状态码处理
+                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                    //返回字符串
+                    String res = unicodeToString(strTmp);
+                    JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                    if(status){
+                        return;
+                    }
                 }
             }
             catch (Exception exception){
 
             }
+            finally {
+                response.close();
+                httpClient.close();
+            }
 
             System.out.println("原cookies = "+shopAccount.getFuziCookie());
 
             // 登陆 Url
             String loginUrl = "https://login.kongfz.com/Pc/Login/account";
-            httpClient = new HttpClient();
+            BasicCookieStore store= new BasicCookieStore();
+            httpClient = HttpClients.custom().setDefaultCookieStore(store).build();
             // 模拟登陆,按实际服务器端要求选用 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);
-                    // 进行登陆后的操作
+            HttpPost httpPost = new HttpPost(loginUrl);
+
+            // 装填参数
+            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
+            nvps.add(new BasicNameValuePair("loginName", shopAccount.getFuziAccount()));
+            nvps.add(new BasicNameValuePair("loginPass", shopAccount.getFuziPassword()));
+            // 设置参数到请求对象中
+            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
+
+            // 设置header信息
+            // 指定报文头【Content-type】、【User-Agent】
+            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
+
+            // 执行请求操作,并拿到结果(同步阻塞)
+            response = httpClient.execute(httpPost);
+            if(response.getStatusLine().getStatusCode() == 302){
+                List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
+                String nCookie = "";
+                for(org.apache.http.cookie.Cookie cookie: cookielist){
+                    String name=cookie.getName();
+                    String value=cookie.getValue();
+                    nCookie += name + "="+value+";";
                 }
+                System.out.println("nCookie:" + nCookie);
+                shopAccount.setFuziCookie(nCookie);
+                shopAccountMapper.updateById(shopAccount);
             }
-            catch (Exception e) {
-                e.printStackTrace();
-            }
+            // 释放链接
+            response.close();
+
         }catch (Exception e) {
             e.printStackTrace();
         }
 
     }
+
     public void dangOrderDeal(ShopAccount shopAccount,ShopOrder shopOrder) throws Exception {
         /**
          * 获取一下店铺的所有静态参数