当前位置: 首页>>代码示例>>Java>>正文


Java Connection.cookies方法代码示例

本文整理汇总了Java中org.jsoup.Connection.cookies方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.cookies方法的具体用法?Java Connection.cookies怎么用?Java Connection.cookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.jsoup.Connection的用法示例。


在下文中一共展示了Connection.cookies方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: loadParse

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * 按照规则,加载解析html
 * @param url		:加载URL
 * @param paramMap	:请求参数
 * @param cookieMap	:请求cookie
 * @param ifPost	:是否使用post请求
 * @param tagMap	:解析规则[0-选择器方式、1-id方式、2-class方式、3-tag]
 * @return
 */
public static Elements loadParse(String url, Map<String, String> paramMap, Map<String, String> cookieMap,
        boolean ifPost, Map<Integer, Set<String>> tagMap) {
    if (!isUrl(url)) {
        return null;
    }
    try {
        // 请求设置
        Connection conn = Jsoup.connect(url);
        conn.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
        if (paramMap != null && !paramMap.isEmpty()) {
            conn.data(paramMap);
        }
        if (cookieMap != null && !cookieMap.isEmpty()) {
            conn.cookies(cookieMap);
        }
        conn.timeout(5000);

        // 发出请求
        Document html = null;
        if (ifPost) {
            html = conn.post();
        } else {
            html = conn.get();
        }

        // 过滤元素
        Elements resultAll = new Elements();
        if (tagMap != null && !tagMap.isEmpty()) {
            for (Entry<Integer, Set<String>> tag : tagMap.entrySet()) {
                int tagType = tag.getKey();
                Set<String> tagNameList = tag.getValue();
                if (tagNameList != null && tagNameList.size() > 0) {
                    for (String tagName : tagNameList) {
                        if (tagType == 0) {
                            Elements resultSelect = html.select(tagName);	// 选择器方式
                            resultAll.addAll(resultSelect);
                        } else if (tagType == 1) {
                            Element resultId = html.getElementById(tagName);	// 元素ID方式
                            resultAll.add(resultId);
                        } else if (tagType == 2) {
                            Elements resultClass = html.getElementsByClass(tagName);	// ClassName方式
                            resultAll.addAll(resultClass);
                        } else if (tagType == 3) {
                            Elements resultTag = html.getElementsByTag(tagName);	// html标签方式
                            resultAll.addAll(resultTag);
                        }
                    }
                }

            }

        } else {
            resultAll = html.getElementsByTag("body");
        }
        return resultAll;
    } catch (IOException e) {
        logger.error("", e);
    }

    return null;
}
 
开发者ID:xuxueli,项目名称:xxl-incubator,代码行数:71,代码来源:JsoupUtil.java

示例2: getConnect

import org.jsoup.Connection; //导入方法依赖的package包/类
public static Connection getConnect(String url, Map<String,String> header, Map<String,String> cookie){
		Connection con;
//		Document doc = null;
		url = url.trim();
		con = Jsoup.connect(url);
		con.ignoreContentType(true);
//		if(!url.startsWith("http:/")){
//			return null;
//		}
		if(url.split(":", 2)[0].equals("https")){
			con.validateTLSCertificates(false);
		}
		if(!(header==null||header.isEmpty())){
			for(Object key : header.keySet()){
				con.header((String) key, header.get(key));
			}
		}else{
			con.header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		}
		if(!(cookie==null||cookie.isEmpty()))
			con.cookies(cookie);
//		try {
//			doc = con.get();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//		e.printStackTrace();logger.error("Exception",e);
//		}
		con.ignoreHttpErrors(true).ignoreContentType(true);
		return con;
	}
 
开发者ID:zrtzrt,项目名称:CrawlerSYS,代码行数:31,代码来源:WebCrawler.java

示例3: getConnection

import org.jsoup.Connection; //导入方法依赖的package包/类
protected Connection getConnection(String url) {
    Connection connection = Jsoup.connect(url);

    Map<String, String> cookies = getCookies();
    if (cookies.size() > 0) {
        connection.cookies(cookies);
    }

    return connection;
}
 
开发者ID:mzlogin,项目名称:guanggoo-android,代码行数:11,代码来源:BaseTask.java

示例4: load

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * 加载页面
 *
 * @param pageLoadInfo
 *
 * @return
 */
public static Document load(PageLoadInfo pageLoadInfo) {
    if (!UrlUtil.isUrl(pageLoadInfo.getUrl())) {
        return null;
    }
    try {
        // 请求设置
        Connection conn = Jsoup.connect(pageLoadInfo.getUrl());
        if (pageLoadInfo.getParamMap() != null && !pageLoadInfo.getParamMap().isEmpty()) {
            conn.data(pageLoadInfo.getParamMap());
        }
        if (pageLoadInfo.getCookieMap() != null && !pageLoadInfo.getCookieMap().isEmpty()) {
            conn.cookies(pageLoadInfo.getCookieMap());
        }
        if (pageLoadInfo.getHeaderMap()!=null && !pageLoadInfo.getHeaderMap().isEmpty()) {
            conn.headers(pageLoadInfo.getHeaderMap());
        }
        if (pageLoadInfo.getUserAgent()!=null) {
            conn.userAgent(pageLoadInfo.getUserAgent());
        }
        if (pageLoadInfo.getReferrer() != null) {
            conn.referrer(pageLoadInfo.getReferrer());
        }
        conn.timeout(pageLoadInfo.getTimeoutMillis());

        // 代理
        if (pageLoadInfo.getProxy() != null) {
            conn.proxy(pageLoadInfo.getProxy());
        }

        // 发出请求
        Document html = null;
        if (pageLoadInfo.getIfPost()) {
            html = conn.post();
        } else {
            html = conn.get();
        }
        return html;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}
 
开发者ID:xuxueli,项目名称:xxl-crawler,代码行数:50,代码来源:JsoupUtil.java

示例5: getDocument

import org.jsoup.Connection; //导入方法依赖的package包/类
public static Document getDocument(String url, boolean loginCoolApk) throws IOException {
    if (!url.startsWith("https://") || !url.startsWith("http://"))
        url = "http://" + url;
    Connection connection = Jsoup.connect(url);
    if (loginCoolApk) {
        connection.cookies(new UserSave().buildWebRequestCookie());
    }
    return connection.get();
}
 
开发者ID:TaRGroup,项目名称:CoolApk-Console,代码行数:10,代码来源:JsoupUtil.java

示例6: fetch

import org.jsoup.Connection; //导入方法依赖的package包/类
public Connection.Response fetch(Parameters params) throws IOException {

        setProperty("sun.net.http.allowRestrictedHeaders", "true");
        setProperty("javax.net.ssl.trustStore", "/etc/ssl/certs/java/cacerts");

        int retryCount = 0;

        while(true) {
            try {
                final Connection connection = Jsoup
                        .connect(params.getUrlToFetch())
                        .method(params.getMethod())

                        .validateTLSCertificates(false)
                        .ignoreHttpErrors(true)
                        .followRedirects(params.isFollowRedirects())
                        .ignoreContentType(GLOBAL_CONFIG.isIgnoringContentType())

                        .userAgent(params.getUserAgent())
                        .referrer(params.getReferrer())

                        .proxy(createProxy(params.getProxy()))
                        .maxBodySize(0)
                        .timeout(params.getTimeout());

                if (!params.getCookie().isEmpty()) {
                    connection.cookies(params.getCookie());
                }

                if (!params.getHeaders().isEmpty()) {
                    connection.headers(params.getCookie());
                }

                if (!params.getRequestBody().isEmpty()) {
                    connection.requestBody(params.getRequestBody());
                }

                System.out.println("\uD83D\uDD3D " + ansi().fg(CYAN).bold().a("fetched page : ").reset() + params.getUrlToFetch());

                return connection.execute();

            } catch(SocketTimeoutException ste) {
                if(retryCount > params.getRetriesOnTimeout()) {
                    throw ste;
                }
                System.out.println("\uD83D\uDD50 " + ansi().fg(YELLOW).bold().a("fetch timeout: ").reset() + "SocketRead time out after " + retryCount++ + ". try");
            }
        }
    }
 
开发者ID:christian-draeger,项目名称:page-content-tester,代码行数:50,代码来源:Fetcher.java


注:本文中的org.jsoup.Connection.cookies方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。