/** * * @Title: init * @Description: TODO 初始化httpclien * @param url * cnzz对应的链接 * @param password * cnzz 对应的密码 * @return * * @return: HttpClient */ public static String init(HttpClient httpclient, String url, String password) { // 建立 httpPost对象 HttpPost httpPost = new HttpPost(url); // 建立一个NameValuePair数组,用于存储欲传送的参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("password", password)); String locationUrl = ""; try { // 设置为utf-8编码 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpPost); // 获取 重定向后的url locationUrl = response.getLastHeader("Location").getValue(); // 设置cookie List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println(cookies.get(i).getName() + ":" + cookies.get(i).getValue() + "-----"); } } // 获得返回体 HttpEntity entity = response.getEntity(); // 获得体内容 String responseHtmQueryPage = IOUtils.toString(entity.getContent()); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return locationUrl; } /** * * @Title: sendRequest * @Description: TODO 根据登陆后跳转的链接发送 第二次请求 * @param httpclient * @param locationUrl * 登陆后重定向的链接 * * @return: url */ public static String sendTwoRequest(HttpClient httpclient, String locationUrl) { String url = ""; try { // 发送get请求 HttpGet httpGet = new HttpGet(locationUrl); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); String responseHtmQueryPage = IOUtils.toString(entity.getContent()); Document document = Jsoup.parse(responseHtmQueryPage); url = "https://web.umeng.com/" + document.select("script").html().split("‘")[1]; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return url; } /** * * @Title: sendRequest * @Description: TODO 发送第三次请求 * @param httpclient * @param url * @return * * */ public static void sendThreeRequest(HttpClient httpclient, String url) { try { // 发送get请求 HttpGet httpGet = new HttpGet(url); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); String responseHtmQueryPage = IOUtils.toString(entity.getContent()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * @Title: sendFourRequest * @Description: TODO 发送第四次请求 及要获取的真正数据的请求 * @param httpclient * @param realUrl * @return * * @return: String */ public static String sendFourRequest(HttpClient httpclient, String realUrl) { // 发送get请求 String responseHtmQueryPage = ""; try { HttpGet httpGet = new HttpGet(realUrl); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); responseHtmQueryPage = IOUtils.toString(entity.getContent()); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return responseHtmQueryPage; }
时间: 2024-10-09 01:47:54