Skip to content

Commit

Permalink
优化抢购逻辑,更加“拟人”,将调用接口返回的cookie,下次调用接口会用上
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrric committed May 20, 2021
1 parent 49b1758 commit 5fb06dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/github/lyrric/conf/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -46,5 +48,9 @@ public class Config {
* 选择的地区代码
*/
public static String regionCode = "5101";
/**
* 调用接口时返回的set-cookie
*/
public static Map<String, String> responseCookie = new HashMap<>();

}
27 changes: 24 additions & 3 deletions src/main/java/com/github/lyrric/service/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -132,7 +135,9 @@ private String get(String path, Map<String, String> 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"))){
Expand All @@ -142,14 +147,30 @@ private String get(String path, Map<String, String> params, Header extHeader) th
}
}

private List<Header>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<Header> getCommonHeader(){
List<Header> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5fb06dc

Please sign in to comment.