package helper; import com.yanzuoguang.util.helper.BaiduHelper; import com.yanzuoguang.util.helper.HttpHelper; import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.thread.ThreadHelper; import helper.vo.BaiduDate; import org.junit.Test; import org.springframework.context.annotation.Profile; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HttpHelperTest { @Test public void test() { long time = System.currentTimeMillis(); Map<String, Object> map = new HashMap<>(); map.put("query", "2021年10月"); map.put("co", ""); map.put("resource_id", "39043"); map.put("t", time); map.put("ie", "utf8"); map.put("oe", "gbk"); map.put("cb", "op_aladdin_callback"); map.put("format", "json"); map.put("tn", "wisetpl"); map.put("cb", "jQuery1102007795278844876075_" + (time - 30)); map.put("_", time - 50); String para = HttpHelper.getUrlParameter(map); String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?"; String fullUrl = url + para; String post = HttpHelper.get(fullUrl, "gbk"); System.out.println(post); Pattern compile = Pattern.compile(".*\\((.*)\\).*"); Matcher matcher = compile.matcher(post); if (matcher.find()) { String group = matcher.group(1); System.out.println(group); BaiduDate result = JsonHelper.deserialize(group, BaiduDate.class); System.out.println(result.getStatus()); } } @Test public void testBaidu() { BaiduHelper.BaiduDate result = BaiduHelper.getBaiduData(new Date()); System.out.println(result.getStatus()); } @Test public void testRequest() { String url = "http://cd2da444e859.ngrok.io/reserveInfo/addSgReserveInfo.do"; String post = "{\"appointmentDate\":\"2021-07-27\",\"arriveAddress\":\"40288ae86ccd4bc8016ccd4c84cf0001\",\"arriveTime\":\"0\",\"cardType\":\"二代身份证\",\"openid\":\"TBD\",\"visitorCardId\":\"43052419871104817X\",\"visitorName\":\"燕子\",\"visitorPhone\":\"13800138000\"}"; String result = HttpHelper.postApplicationJSON(url, post); System.out.println(result); } @Test public void testReq() throws Exception { String result = HttpHelper.postRequest("http://127.0.0.1:6903/test/wait/11500", new ArrayList<>()); System.out.println(result); } @Test public void testReqWait() throws Exception { String result = HttpHelper.postRequest("http://127.0.0.1:6903/test/wait/20000", new ArrayList<>()); System.out.println(result); } @Test public void testReq1() throws Exception { String result = HttpHelper.post("http://127.0.0.1:6903/test/wait/11500", StringHelper.EMPTY); System.out.println(result); } @Test public void testReqWait1() throws Exception { String result = HttpHelper.post("http://127.0.0.1:6903/test/wait/20000", StringHelper.EMPTY); System.out.println(result); } @Test @Profile({"mrod", "prod"}) public void testHelper() throws Exception { String result = HttpHelper.post("http://127.0.0.1:6903/test/wait/20000", StringHelper.EMPTY); System.out.println(result); } @Test public void testThread() throws Exception { int maxThread = 200; int maxCount = 1000000; Request req = new Request("http://10.150.2.3:6888/system/login/heart", maxThread, maxCount); // Request req = new Request("http://127.0.0.1:6907/login/heart", maxThread, maxCount); long totalStart = System.currentTimeMillis(); for (int i = 0; i < req.thread; i++) { ThreadHelper.runThread(() -> { while (true) { synchronized (req) { if (req.start < req.max) { req.start++; } else { return; } } long start = System.currentTimeMillis(); boolean isError = false; try { String s = HttpHelper.get(req.url); } catch (Exception ex) { isError = true; } finally { long end = System.currentTimeMillis(); long time = end - start; synchronized (req) { req.maxTime = Math.max(time, req.maxTime); req.minTime = Math.max(time, req.maxTime); if (isError) { req.error++; } else { req.ok++; } } } } }); } while (req.max > req.ok + req.error) { ThreadHelper.sleep(100); System.out.println(req); } long totalEnd = System.currentTimeMillis(); System.out.println("总用时:" + (totalEnd - totalStart)); } class Request { String url; int thread; int max; volatile int start; volatile int ok; volatile int error; volatile long maxTime; volatile long minTime; public Request(String url, int thread, int max) { this.url = url; this.thread = thread; this.max = max; } @Override public String toString() { return "Request{" + "url='" + url + '\'' + ", thread=" + thread + ", max=" + max + ", start=" + start + ", ok=" + ok + ", error=" + error + ", maxTime=" + maxTime + ", minTime=" + minTime + '}'; } } }