標籤:儲存 cnzz ace pass eal string 類比 generated sel
/** * * @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);// 擷取 重新導向後的urllocationUrl = response.getLastHeader("Location").getValue();// 設定cookieList<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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return responseHtmQueryPage;}
cnzz 類比請求登入(傳入url get data ) demo