码迷,mamicode.com
首页 > Web开发 > 详细

需登录账号与密码的网页爬取demo

时间:2017-08-17 12:34:16      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:windows   entity   com   exception   pass   turn   exe   eva   nec   

public static String connect(String dataUrl){     
		String result = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost("http://rs.260g.com:8090/index.php?c=user&a=login");
			httpPost.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
			// 创建名/值组列表 
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			parameters.add(new BasicNameValuePair("username", "******"));
			parameters.add(new BasicNameValuePair("password", "*****"));
			parameters.add(new BasicNameValuePair("rememberMe", "****"));
			// 创建UrlEncodedFormEntity对象 
			UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters);
			httpPost.setEntity(formEntiry);
			HttpResponse httpResponse = httpclient.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();			
			result = IOUtils.toString(entity.getContent());
			HttpGet searchHttpGet = new HttpGet(dataUrl);
			searchHttpGet.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
			httpResponse = httpclient.execute(searchHttpGet);
			result = IOUtils.toString(httpResponse.getEntity().getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;    
} 

  

需登录账号与密码的网页爬取demo

标签:windows   entity   com   exception   pass   turn   exe   eva   nec   

原文地址:http://www.cnblogs.com/ipetergo/p/7380535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!