From 5fb06dc823b9a6f6e85599bb60e6dfccfaa59720 Mon Sep 17 00:00:00 2001 From: xuanxin Date: Thu, 20 May 2021 22:59:01 +0800 Subject: [PATCH] =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=8A=A2=E8=B4=AD?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=9B=B4=E5=8A=A0=E2=80=9C=E6=8B=9F?= =?UTF-8?q?=E4=BA=BA=E2=80=9D=EF=BC=8C=E5=B0=86=E8=B0=83=E7=94=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=BF=94=E5=9B=9E=E7=9A=84cookie=EF=BC=8C=E4=B8=8B?= =?UTF-8?q?=E6=AC=A1=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3=E4=BC=9A=E7=94=A8?= =?UTF-8?q?=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/lyrric/conf/Config.java | 6 +++++ .../github/lyrric/service/HttpService.java | 27 ++++++++++++++++--- .../github/lyrric/service/SecKillService.java | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/lyrric/conf/Config.java b/src/main/java/com/github/lyrric/conf/Config.java index 8420b90..c97852b 100644 --- a/src/main/java/com/github/lyrric/conf/Config.java +++ b/src/main/java/com/github/lyrric/conf/Config.java @@ -2,7 +2,9 @@ import com.github.lyrric.model.Member; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Created on 2020-07-23. @@ -46,5 +48,9 @@ public class Config { * 选择的地区代码 */ public static String regionCode = "5101"; + /** + * 调用接口时返回的set-cookie + */ + public static Map responseCookie = new HashMap<>(); } diff --git a/src/main/java/com/github/lyrric/service/HttpService.java b/src/main/java/com/github/lyrric/service/HttpService.java index 8f91258..6c8e5ca 100644 --- a/src/main/java/com/github/lyrric/service/HttpService.java +++ b/src/main/java/com/github/lyrric/service/HttpService.java @@ -7,10 +7,12 @@ import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; +import org.apache.http.message.BufferedHeader; import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -21,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * Created on 2020-07-22. @@ -132,7 +135,9 @@ private String get(String path, Map params, Header extHeader) th } get.setHeaders(headers.toArray(new Header[0])); CloseableHttpClient httpClient = HttpClients.createDefault(); - HttpEntity httpEntity = httpClient.execute(get).getEntity(); + CloseableHttpResponse response = httpClient.execute(get); + dealHeader(response); + HttpEntity httpEntity = response.getEntity(); String json = EntityUtils.toString(httpEntity, StandardCharsets.UTF_8); JSONObject jsonObject = JSONObject.parseObject(json); if("0000".equals(jsonObject.get("code"))){ @@ -142,14 +147,30 @@ private String get(String path, Map params, Header extHeader) th } } - private List
getCommonHeader(){ + private void dealHeader(CloseableHttpResponse response){ + Header[] responseHeaders = response.getHeaders("Set-Cookie"); + if (responseHeaders.length > 0) { + for (Header responseHeader : responseHeaders) { + String cookie = ((BufferedHeader) responseHeader).getBuffer().toString().split(";")[0].split(":")[1].trim(); + String[] split = cookie.split("="); + Config.responseCookie.put(split[0], cookie); + } + } + } + + private List
getCommonHeader(){ List
headers = new ArrayList<>(); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; SM-N960F Build/JLS36C; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36 MMWEBID/1042 MicroMessenger/7.0.15.1680(0x27000F34) Process/appbrand0 WeChat/arm32 NetType/WIFI Language/zh_CN ABI/arm32")); headers.add(new BasicHeader("Referer", "https://servicewechat.com/wxff8cad2e9bf18719/2/page-frame.html")); headers.add(new BasicHeader("tk", Config.tk)); headers.add(new BasicHeader("Accept","application/json, text/plain, */*")); headers.add(new BasicHeader("Host","miaomiao.scmttec.com")); - headers.add(new BasicHeader("Cookie",Config.cookies)); + String cookie = Config.cookies; + if(!Config.responseCookie.isEmpty()){ + String join = String.join("; ", new ArrayList<>(Config.responseCookie.values())); + cookie = join + "; " + cookie; + } + headers.add(new BasicHeader("Cookie", cookie)); return headers; } diff --git a/src/main/java/com/github/lyrric/service/SecKillService.java b/src/main/java/com/github/lyrric/service/SecKillService.java index ca22dcf..098157c 100644 --- a/src/main/java/com/github/lyrric/service/SecKillService.java +++ b/src/main/java/com/github/lyrric/service/SecKillService.java @@ -88,7 +88,7 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF } while (orderId.get() == null); }; - for (int i = 0; i < 20; i++) { + for (int i = 0; i < 1; i++) { service.submit(runnable); } service.shutdown();