1. 程式人生 > >cnzz 模擬請求登錄(傳入url get data ) demo

cnzz 模擬請求登錄(傳入url get data ) demo

iou 根據 client last sel 建立 oid 獲取 ioutils

/**
	 * 
	 * @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;

	}

  

cnzz 模擬請求登錄(傳入url get data ) demo