获取网页内容---"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
原创粉丝点击