public static void getSsqNumber() { int pageNo = 1; boolean flag = true; while (flag) { try { String test = HttpUtils.sendHttpsGet("pageNo=" + pageNo + "&pageSize=30"); JSONObject jsonObject = JSONObject.parseObject(test); JSONArray jsonArray = jsonObject.getJSONArray("result"); if (jsonArray.size() <= 30) { flag = false; } else { pageNo++; } for (Object ob : jsonArray) { JSONObject voJson = JSONObject.parseObject(JSONObject.toJSONString(ob)); //期号 voJson.getString("code"); //开奖时间 voJson.getString("date"); //星期几 voJson.getString("week"); //红球号 voJson.getString("red"); //篮球号 voJson.getString("blue"); //销售额 voJson.getString("sales"); //奖池 voJson.getString("poolmoney"); //yi等奖中奖地区及注数 voJson.getString("content"); } } catch (Exception e) { e.printStackTrace(); } } }
/** * 向指定 URL 发送GET方法的请求 */ public static String sendHttpsGet(String url) throws Exception{ String requestUrl = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet(requestUrl); CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println("Response Body : " + responseBody); return responseBody; } finally { response.close(); } } finally { httpClient.close(); } }