html dom jsoup httpclient

阅读更多
xml dom 对大多数java程序员来说并不陌生,但是html dom可能就不是那么面善了。

jsoup 就是用来处理 html dom的一个组件。其与dom4j、xstream等xml dom组件类似。

jsoup加载完html文本生成document对象之后,用来操作dom的通用操作就可以使用了。如:getElementById、getElementsByName等。

代码:加载html文本为dom对象,获取id为username的元素的属性value的值
Document doc = Jsoup.parse(htmlString);
String username = doc.getElementById("username").attr("value");


另附上 httpclient jar包 jsoup jar包

httpclient设置头部参数,以及获取头部参数,设置消息体参数的代码:

httpclient设置头部参数
/**
	 * 设置请求头
	 * @param post
	 */
	private static void setHeaders(HttpPost post) {
		post.setHeader("Accept", "text/html, application/xhtml+xml, */*");
		post.setHeader("Accept-Language", "zh-CN");
		post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		post.setHeader("Accept-Encoding", "gzip, deflate");
	
		post.setHeader("DNT", "1");
		post.setHeader("Connection", "Keep-Alive");
		post.setHeader("Cache-Control", "no-cache");
		post.setHeader("Cookie", Cookie);
	}
/**
	 * 设置请求头
	 * @param get
	 */
	private static void setHeaders(HttpGet get) {
		get.setHeader("Accept", "text/html, application/xhtml+xml, */*");
		get.setHeader("Accept-Language", "zh-CN");
		get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
		get.setHeader("Content-Type", "application/x-www-form-urlencoded");
		get.setHeader("Accept-Encoding", "gzip, deflate");
		
		get.setHeader("DNT", "1");
		get.setHeader("Connection", "Keep-Alive");
		get.setHeader("Cache-Control", "no-cache");
		get.setHeader("Cookie", Cookie);
	}


httpclient发送get请求
private static String getSessionId() throws IOException, Exception {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet("http://url/");
		HttpResponse responsex = client.execute(get);
		Header[] headers = responsex.getAllHeaders();
		for (int i = 0; i < headers.length; i++) {
			Header header = headers[i];
			String name = header.getName();
			String value = header.getValue();
			if("Set-Cookie".equals(name)){
				String[] vals = value.split(";");
				for (int j = 0; j < vals.length; j++) {
					String val = vals[j];
					if(val.contains("ASP.NET_SessionId")){
						Cookie = val;
					}
				}
			}
		}
		if(!responsex.getStatusLine().toString().contains("HTTP/1.1 200 OK")){
			throw new RuntimeException("某某失败");
		}
		HttpEntity entityx = responsex.getEntity();
		String entityMsgx = EntityUtils.toString(entityx);
		//解析html dom 得到输入参数
		Document doc = Jsoup.parse(entityMsgx);
		resetAllParams();
		__VIEWSTATE = doc.getElementById("__VIEWSTATE").attr("value");
		__EVENTVALIDATION = doc.getElementById("__EVENTVALIDATION").attr("value");
		isOpen = doc.getElementById("isOpen").attr("value");
		btlogin = doc.getElementById("btlogin").attr("value");
		
		return Cookie;
	}


发送post请求
/**
	 * 登录
	 * @param username
	 * @param password
	 * @throws IOException
	 * @throws Exception
	 */
	public static boolean login(String username, String password) throws IOException, Exception {
		//先访问首页获取sessionid
		getSessionId();
		//执行登陆过程
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://loginurl");
		post.setHeader("Referer", "http://loginurl");
		setHeaders(post);
		
		// 登录表单的信息
		List qparams = new ArrayList();
		qparams.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
		qparams.add(new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION));
		qparams.add(new BasicNameValuePair("isOpen", isOpen));
		qparams.add(new BasicNameValuePair("Text1", username));
		qparams.add(new BasicNameValuePair("Password1", password));
		qparams.add(new BasicNameValuePair("btlogin", btlogin));

		UrlEncodedFormEntity params = new UrlEncodedFormEntity(qparams, "utf-8");
		post.setEntity(params);
		// 相当于按了下确定登录的按钮,也就是浏览器调转了
		HttpResponse response = client.execute(post);
		if(!response.getStatusLine().toString().contains("HTTP/1.1 200 OK")){
			throw new RuntimeException("离校系统登陆失败");
		}
		
		HttpEntity entity = response.getEntity();
		String entityMsg = EntityUtils.toString(entity);
		if(entityMsg.contains("该用户不存在或用户名密码错误")){
			throw new RuntimeException("该用户不存在或用户名密码错误");
		}
		return true;
	}
  • jar.zip (1.4 MB)
  • 下载次数: 2

你可能感兴趣的:(html,dom,jsoup,httpclient,post)