获取网页内容---"User-Agent"
来源:互联网 发布:澳门娱乐网络博客 编辑:程序博客网 时间:2024/06/02 04:29
声明:文章内容全都是自己的学习总结,如有不对的地方请大家帮忙指出。有需要沟通交流的可加我QQ群:425120333 主要是通过配置文件加载User-Agent信息,为后面的获取获取网页提供帮助。
import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory; public class BrowserUtil { private static final Log logger = LogFactory.getLog(BrowserUtil.class); private static List<String> browserList = BrowserUtil.loadBrowser(); private static int index = -1; private static List<String> loadBrowser() { List<String> list = new ArrayList<String>(); String filePath = StringUtil.getClassPath() + "browser.txt"; File file = new File(filePath); if (!file.exists()) { return list; } BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line = null; while ((line = br.readLine()) != null) { list.add(line); } } catch (FileNotFoundException e) { logger.error(BrowserUtil.class, e); } catch (IOException e) { logger.error(BrowserUtil.class, e); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { logger.error(BrowserUtil.class, e); } } logger.info("browser list size :" + list.size()); return list; } /** * @introduction: * @param: * @return: */ public static synchronized String getUserAgent() { index++; if (index >= browserList.size()) { index = -1; } return browserList.get(index); }}
代码中用到的文件browser.txt里面存放的内容如下:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0)Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; Sleipnir/2.9.8)Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like GeckoMozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
可以根据自己的需求添加。
0 0
- 获取网页内容---"User-Agent"
- User-agent获取手机类型
- 获取User-Agent
- 获取IE的user agent
- Android获取浏览器User Agent
- 获取user-agent并区分
- Asp.net获取User-agent
- Android获取浏览器User Agent
- 工具------随机获取User-Agent
- 使用User-Agent获取操作系统名称
- 获取 Windows Phone 的 User-Agent 字符串
- 获取微信版本号 user agent
- webapp根据User-Agent获取手机信息
- User-Agent头部文件获取方法
- java 通过user-agent获取客户端
- User Agent
- user agent
- User Agent
- NFS 网络文件系统 安装配置
- poj2942 Knights of the Round Table
- Android热修复原理(HotFix)初涉
- hdu 2612 Find a way
- 递归
- 获取网页内容---"User-Agent"
- SElinux
- Spark 调度架构原理深度解析
- bzoj 2151: 种树 贪心+优先队列
- ubuntu下如何显示自己的ip
- 所谓好运
- 算法设计与分析练习题(拯救Amaze)
- Caffe在Ubuntu 14.04 64bit 下的安装
- css选择器